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

How to set application name in a Postgresql JDBC url?

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

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

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

我也尝试过此网址,但没有成功。

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

正确的参数名称是什么?

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


查看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

因此,请尝试:

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

请注意,有一些注释表明这在9.1版驱动程序中已被破坏。 鉴于该版本已有5年以上的历史了,因此您仍然应该升级到较新的版本。 检查https://jdbc.postgresql.org/以获取最新版本并使用它。