2

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.

3
  • For cp 1252 your string ÐБВГ is encoded in hex as D0,90,D0,91,D0,92,D0,93. When interpreted as UTF-8 it gives АБВГ. So after getting data from data base you need to convert it from UTF-8 to code page used for displaying. Commented Dec 15, 2020 at 8:36
  • @Daniel but it looks wrong in the database. I can't use sorting in my queries. Commented Dec 15, 2020 at 8:49
  • You have unpaired conversions [input]-->{convert to UTF-8}-->[DB]-->[output] or [input]-->{convert to UTF-8}-->{convert to UTF-8}-->[DB]-->{convert from UTF-8}-->[output] and should be [input]-->{convert to UTF-8}-->[DB]-->{convert from UTF-8}-->[output]. Maybe you need to check connecion parameters or don't convert data before sending to database or viewer uses wrong encoding. Commented Dec 15, 2020 at 9:48

1 Answer 1

5

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

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

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.