如何在 Postgresql JDBC url 中设置应用程序名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19224934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:08:28  来源:igfitidea点击:

How to set application name in a Postgresql JDBC url?

postgresqlurljdbcpostgresql-9.2

提问by Stephan

I want to set the application name of the connections of my application. So when I list the rows in pg_stat_activityI can have a non empty application_namecolumn.

我想设置我的应用程序连接的应用程序名称。所以当我列出行时,pg_stat_activity我可以有一个非空application_name列。

I have setup the following JDBC url for connecting to my Postgresql database:

我已经设置了以下 JDBC url 来连接到我的 Postgresql 数据库:

jdbc:postgresql://localhost:5435/MyDB?application-name=MyApp

I have tried also this url with no more success.

我也试过这个网址,但没有成功。

jdbc:postgresql://localhost:5435/MyDB?application_name=MyApp

What is the correct parameter name ?

什么是正确的参数名称?

Here is my JDBC driver version: 9.1-901.jdbc4

这是我的 JDBC 驱动程序版本: 9.1-901.jdbc4

回答by Mark Rotteveel

Looking at the PostgreSQL JDBC 9.1 documentation, connection parameters, the correct property name in the JDBC url is ApplicationName:

查看PostgreSQL JDBC 9.1 文档,连接参数,JDBC url 中正确的属性名称是ApplicationName

ApplicationName = String

Specifies the name of the application that is using the connection. This allows a database administrator to see what applications are connected to the server and what resources they are using through views like pg_stat_activity

ApplicationName = String

指定使用连接的应用程序的名称。这允许数据库管理员通过类似的视图查看哪些应用程序连接到服务器以及它们正在使用哪些资源pg_stat_activity

So try:

所以尝试:

jdbc:postgresql://localhost:5435/MyDB?ApplicationName=MyApp

Be aware some comments suggest that this is broken in the 9.1 version driver. Given it is a more than 5 year old version, you should upgrade to a newer version anyway. Check https://jdbc.postgresql.org/for the latest version and use that.

请注意,一些评论表明这在 9.1 版驱动程序中已损坏。鉴于它是一个超过 5 年的旧版本,无论如何您都应该升级到更新的版本。检查https://jdbc.postgresql.org/以获取最新版本并使用它。