I am working on a parser that sniffs network traffic and parses the messages of PostgreSQL (Server version 9.6.11). I need to be able to pass the application_name parameter in the login flow (before it returns authentication successful) meaning I cannot rely on "SET application_name" etc.
Is this possible with JDBC? For instance, using ODBC it is possible by setting the libpq parameters: application_name=myapp. And the startup message (https://www.postgresql.org/docs/9.5/protocol-message-formats.html) will contain the parameter alongside username and database.
I am using Wireshark to verify it.
I have tried reading some suggestions here on SO but all solutions rely on setting the application name after the login is complete.
I've also tried via DBeaver's connection settings -> Driver properties -> ApplicationName and I'm getting the same results.
Properties props = new Properties();
props.setProperty("user",user);
props.setProperty("password",password);
props.setProperty("ssl","true");
props.setProperty("sslmode","disable");
props.setProperty("ApplicationName","my-app");
try (Connection connection = DriverManager.getConnection("jdbc:postgresql://"+host+
":5432/" + db , props)) {
System.out.println("Java JDBC PostgreSQL Example");
Class.forName("org.postgresql.Driver");
System.out.println("Connected to PostgreSQL database!");
Statement statement = connection.createStatement();
}
or this,
Properties props = new Properties();
props.setProperty("user",user);
props.setProperty("password",password);
props.setProperty("ssl","true");
props.setProperty("sslmode","disable");
try (Connection connection = DriverManager.getConnection("jdbc:postgresql://"+host+
":5432/" + db+ "?application_name=my-app" , props)) {
System.out.println("Java JDBC PostgreSQL Example");
Class.forName("org.postgresql.Driver");
System.out.println("Connected to PostgreSQL database!");
Statement statement = connection.createStatement();
}
I am looking for a way to replicate the ODBC behavior.
This is not the same question as How to set application name in a Postgresql JDBC url?. I'm asking if its possible to have this parameter passed before the login protocol is completed. When I look at the messages with Wireshark, I can clearly see that with JDBC all methods of passing this parameter are broken down to this message sequence:
- login request
- login success
- SET application_name = my-app
in ODBC its like this:
- login request (contains the parameter application_name)
- login success