Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set application name in a Postgresql JDBC url?

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

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

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

like image 382
Stephan Avatar asked Oct 07 '13 12:10

Stephan


People also ask

How connect PostgreSQL to jdbc?

To summarize how you can connect to a PostgreSQL database server, you add the PostgreSQL driver in your classpath. Use DriverManager and provide the connection string, username, and password to connect to the server. You then execute queries using the established connection.

What is database URL for PostgreSQL?

jdbc:postgresql:// host / database.

Is it possible to specify the schema when connecting to Postgres with jdbc?

We can use JDBC URL to specify all kinds of parameters during connection setup. The usual parameters are database type, address, port, database name, etc. Since Postgres version 9.4. there is added support for specifying the current schema using URL.


1 Answers

Looking at the PostgreSQL JDBC 9.1 documentation, connection parameters, the correct property name in the JDBC url is 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

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.

like image 179
Mark Rotteveel Avatar answered Oct 18 '22 05:10

Mark Rotteveel