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?

2
  • 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. Commented Jun 30, 2014 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. Commented Jul 1, 2014 at 21:23

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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]).
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.