How to set task_name for postgres connections?

I am using the tomcat connection pool org.apache.tomcat.jdbc.pool.DataSource . Connections are displayed in my pg_stat_activity database with an empty application_name .

How can I set this application name in my java application, so I know where the connection is coming from (since there will be several applications accessing the same db)?

+6
source share
3 answers

You can specify the name of the application in the connection string.
The documentation is here .

Example:

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

Take care : parameter names are case sensitive.

+8
source

Use the set command:

 set application_name to my_application; 
+6
source

You can add this to the JDBC URL that you use in the connection pool definition:

 jdbc:postgresql://localhost/postgres?ApplicationName=my_app 

If you want to change it dynamically, for example. to reflect the various modules inside your application, you can use the SET command as shown by the wedge.

+2
source

Source: https://habr.com/ru/post/987493/


All Articles