41

Getting the below exception while executing SELECT query for a particular row on that table

ERROR:  character with byte sequence 0xc2 0x81 in encoding "UTF8" has no equivalent in encoding "WIN1252"

One of the column in that row contains Japanese character which has been encoded with UTF-8 and inserted into it.

Is there any fix for this issue?

2

8 Answers 8

75

You should know what encoding is used in your database.

SHOW server_encoding;

When you connect to your database you can specify what encoding should your client use:

SET client_encoding TO 'UTF8';

If server and client encoding differ, the database driver tries to translate between those two encoding. When it can not find an equivalent character, the error is thrown.

So server encoding and client encoding should be the same to avoid problems like yours.

To fix your problem:

  • connect to your database
  • set client_encoding to UTF8
  • update the row with Japanese characters

To avoid this problem in the future remember to set client_encoding to proper value when you connect to the database.

Check the documentation on Supported Character Sets.

3
  • 1
    both are in UTF8 JapanDB=# SHOW server_encoding; server_encoding ----------------- UTF8 (1 row) JapanDB=# show client_encoding; client_encoding ----------------- UTF8 (1 row) Jul 21, 2016 at 7:04
  • Is it possible to default the client_encoding to UTF8 in psql so we don't have to execute it every time? May 20, 2021 at 17:47
  • 1
    @MatthewMoisen Yes. Use PGCLIENTENCODING environment variable OR client_encoding configuration variable. See Automatic Character Set Conversion Between Server and Client.
    – Adam
    Jun 3, 2021 at 19:23
15

see here

I've encountered this error "ERROR: character with byte sequence <...> in encoding "UTF8" has no equivalent in encoding "WIN1252";" while using MySQL Workbench to migrate data from PostgreSQL to MySQL. I was not sure why this was happening because the target MySQL database had UTF8 encoding and I thought that everything can be mapped to it.

The real root cause of this problem turned out to be the PostgreSQL driver I used to connect to the source DB and specified in the Workbench which was PostgreSQL ODBC Driver (ANSI) and after I changed it to PostgreSQL ODBC Driver (UNICODE) everything worked fine.

1
7

For me I was importing data from a csv file, I needed to connect to the database then change the client encoding from WIN1252 to UTF8, then import the data from the csv file:

  • psql -h localhost -p 5432 -d timetrex -U postgres; // connect to the database
  • set client_encoding to UTF8;
  • show client_encoding; // this command will show the current encoding
  • While you still inside the database you need to import the file this way: COPY users FROM 'D:/Downloads/users_dump.csv' (format CSV , header true, DELIMITER ',' ,ENCODING 'UTF8') ; // copy the data from the csv file
0

I got the error when Crystal Reports saw this set of characters in a field: [‎18/‎03/‎2019 2:20 PM]

In the end I just changed the content of that field, but unless I can find a decent PostgreSQL ODBC Driver that is designed for UTF8 I will probably see this issue pop up every few weeks, and will need to data cleanse.

0

It happened to me in crystal reports...

I changed the content of the column of my view that was generating error...

I discovered that character "0xc2 0x81" is equal "chr(128)" - http://lwp.interglacial.com/appf_01.htm - so i've replaced this character, like this: (replace((mi.complemento)::text, chr(128), 'C'::text))::character varying(400) AS complemento

cheers!

0
0

I was facing the same error but changing from ANSI(X64) to Unicode(x64) in my ODBC conn fixed my issue.

0

Got this error when using COPY command, postgres uses client default encoding if you don't specify it. So in this case to fix the issue:

COPY table_name FROM 'a:\file' ENCODING 'utf8'
0

This links back to what odbc driver we are using in ControlPanel\All control Panel Items\Administrative tools. Instead of having it as a simple ANSI change it to Unicode NOW!...Cheers

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Jul 26, 2022 at 15:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.