„New” Postgres JDBC drivers check database engine version and if it’s not below 7.3 they will ignore charSet declaration. It breaks encoding of 8-bit characters if database has SQL_ASCII encoding (which basically means DONT_CARE_ANYTHING_GOES). If your database is set to SQL_ASCII and in reality it stores e.g. Windows-1250 (CP1250) characters, JDBC driver will assume incorrect encoding and diacritical characters will be broken.
Even though I understand the reasons given by Postgres developers for this (SQL_ASCII is NOT supposed to be used with anything other than 7-bit characters and it is obsolete), the solution they’re suggesting (to dump the db, and convert it to UTF-8) is not always possible. For instance, I would have a hard time telling my customer to do it, especially because their old software might break.
So I’ve patched the JDBC driver to accept charSet directive in JDBC URLs and also added a method setCharset to PGSimpleDataSource.
Here you can download it: Postgres JDBC driver – charSet patched
I downloaded your patched PostgreSQL JDBC driver, and used it with my old SQL_ASCII database. Everything worked great!! Thank you very much!! Any chance you could post or send me the source code with your modifications? Thanks in advance.
Hi Eduardo,
I’m glad it works for you. I’ve only patched 3 files, here goes the diff:
This has been tremendously helpful, as recently Postgres (> 9.4) decided to throw an error for non-utf-8 characters. With our legacy db, this would not work with JDBC insisting on UTF-8.
Thanks again for the patch.
Hi,
Looks like when entering UTF-8 strings through this driver using preparedStatement.setString method, it is messing up the strings. Putting T?kia instead of Tëkia, e.g.,
Any thoughts?
Thanks.
Well, it’s been 6 years since I worked on it, so I don’t remember too much 🙁 I only did SELECT * queries with patched driver (my use case was to mirror the DB to some place and work from there), so I didn’t even try prepared statements. I’d guess there’s a reason Postgres devs abandoned the option I restored with this patch. But I’m afraid there won’t be an easy solution for You unless You take JDBC driver’s source code, patch it (diff is in one on my previous comments) and debug it Yourself. It’s not that difficult once You start with it 🙂