3

I have an application written in Java that connects to a Postgresql 9.3 database.

  1. The database I'm connecting to is created with UTF-8 enconding.
  2. The locale for the server where the database is installed is es-uy.UTF-8.
  3. All the information submitted from the application (web app running in Tomcat 7) is also in UTF-8.

Everything works according to the encoding settings except the JDBC exception messages. If I try to connect to the database with a wrong password I get:

org.postgresql.util.PSQLException: FATAL: la autentificaci��n password fall�� para el usuario ��postgres��

If I try the same from psql or pgadmin I get the message with the correct encoding:

psql: FATAL:  la autentificación password falló para el usuario «postgres»

What is wrong here?

lufte
  • 1,283
  • 1
  • 11
  • 21
  • Where are you seeing the exception messages? My guess is that it's that part that's the problem, and nothing to do with JDBC itself. – Jon Skeet Jun 30 '14 at 18:43
  • The exception messages were incorrectly encoded both in netbeans console and in tomcat log files. It seems now that it really is the JDBC's fault, but I agree that it was unlikely. – lufte Jul 01 '14 at 21:23

2 Answers2

3

It looks like PgJDBC sets the client encoding once it's authenticated and connected, not in the protocol startup packet. So the server doesn't know the encoding PgJDBC expects and uses its default encoding - but PgJDBC must be ignoring the server's notification about the server encoding.

That's clearly a bug; I'll chase it up if time permits.

It's possible that I have the protocol flow order wrong here, though. I think the startup packet carries the encoding and is sent pre-auth. If I'm wrong then this is a protocol issue that requires a protocol rev to fix - not quick or simple.

I'll check. Meanwhile here's the pgjdbc bug.

(On a side note, PostgreSQL also mixes different text encodings in its log files, creating a horrible mess, because it emits log messages to the logs in the client_encoding. I've looked at fixing this before but there's been resistance to what I see as the only possible fixes - a log file per database and logging in the database default encoding, or preferably just logging everything in utf-8).

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • I can now confirm that this is right because if I authenticate correctly, all subsequent error messages are encoded in UTF-8. For example: I authenticated, ran the query "SELECT 1/0" and got an exception with the message "ERROR: división por cero" (note the "ó"). Thanks for your answer. – lufte Jul 01 '14 at 21:19
  • 1
    I've come across another instance of this issue, with an explanation. When `max_connections` is low and the clients exhaust the server-allowed connections, the driver throws an exception in `ConnectionFactoryImpl.readStartupMessages`. In that case, the error message is not encoded in UTF-8 (probably win1252 in my case), _even if both client and server are set to use UTF-8_. The hint came after setting a breakpoint in the driver code and reading "Le nombre de connexions r" in the character buffer, which led me to suspect "r" stood for "réservées" (reserved [connections]). – AbVog Feb 08 '17 at 15:23
0

I added the driver in the application.properties, nd it works

spring.datasource.driver-class-name=org.postgresql.Driver