Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 698
Raamesh Keerthi Avatar asked Jul 20 '16 12:07

Raamesh Keerthi


People also ask

What UTF 8 means?

UTF-8 (UCS Transformation Format 8) is the World Wide Web's most common character encoding. Each character is represented by one to four bytes. UTF-8 is backward-compatible with ASCII and can represent any standard Unicode character.


1 Answers

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.

like image 58
Adam Avatar answered Sep 28 '22 05:09

Adam