Dialecticus
Dialecticus

Reputation: 16769

Using UTF8 in Postgresql ODBC connection

I'm trying to insert some UTF-8 strings into PostgreSQL database. I'm using Visual C++ and MFC (this bit probably not important) and a project setting "Use Multi-Byte Character Set" (trying to switch database in an old legacy app). So when I execute some INSERT command with some text in Cyrillic "АБВГ" I expect to see this text in database, but I'm seeing this instead (in DBeaver): "ÐБВГ". I insert this text by converting the string "\xC0\xC1\xC2\xC3" from code page 1251 to CP_UTF8.

When I change the system setting "Language for non-Unicode programs" from English to some Cyrillic, like Russian, the text that is actually inserted is no longer "ÐБВГ", but "АБВГ". Postgres ODBC driver apparently uses CP_ACP to interpret my multi-byte strings. Indeed, if I now try to insert "\xC0\xC1\xC2\xC3" directly (without conversion to UTF-8), I do see "АБВГ" in database. But I need to insert UTF-8 strings, not a subset from a code page.

How do I instruct the Postgres ODBC driver to interpret my strings as UTF-8, and ignore the "Language for non-Unicode programs" system setting?

In PSQL console both server_encoding and client_encoding are set to UTF8.

Upvotes: 2

Views: 4008

Answers (1)

Dialecticus
Dialecticus

Reputation: 16769

Change your ODBC DSN connection string to include this: ConnSettings=SET CLIENT_ENCODING TO 'UTF8';

Upvotes: 5

Related Questions