11

I create a connection to a postgres 9 database using the stardard JDBC driver.

...
Connection myCon = DriverManager.getConnection("jdbc:postgresql://localhost/test?&user=test&password=test"); 
...

When I check the server status with PgAdmin and display all database sessions, I can see that the "Application Name" is not set for my Session. Is there a way to set the application name in the JDBC connection?

casperOne
  • 73,706
  • 19
  • 184
  • 253
markus
  • 6,258
  • 13
  • 41
  • 68
  • Time to answer my own question, it can be SET with the following code: stmt = Con.prepareStatement("SET application_name = 'myApp'"); stmt.execute(); – markus Aug 22 '11 at 11:23

1 Answers1

13

That is possible to set application name as connection parameter since Postgres JDBC 9.1dev-900:

Add support for setting application_name on both connection startup and later through Connection.setClientInfo. (jurka)

ex: jdbc:postgresql://localhost:5432/DbName?ApplicationName=MyApp

or Connection.setClientInfo("ApplicationName", "My App")

For previous versions you could do this by setting application_name runtime parameter:

s.execute("SET application_name TO 'MyApp'");
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 2
    Just in case it would be helpful for someone : this can be done specifically for Oracle databases with an additionnal property : ((BasicDataSource) datasource).addConnectionProperty("v$session.program", "Explicit connection label"). This will then be showed in the v$session Oracle view. Of course this ways works only for Oracle and your JDBC solution - whom i was unaware of - should be more robust. – SRG May 06 '12 at 13:50