22021 Character Not In Repertoire
When a client sends or receives text data to the server, the following error may occur:
ERROR: invalid byte sequence for encoding "UTF8": 0xb2
Possible causes:
- The encoding of the client system environment does not match the encoding parameter of the database client (e.g.
cp936
for the client system environment andUTF8
for the database client). - The encoding of the executed SQL script file does not match the encoding parameter of the database client.
- the text stream contains binary data like NUL bytes that are not allowed within a string.
Solution:
On Linux, you can use locale
to view the encoding of the client system environment:
$ locale -k charmap
charmap="UTF-8"
On Linux, you can use file
to view the encoding of the SQL script file:
$ file --mime test.sql
test.sql: text/plain; charset=utf-8
On Windows, you can use chcp
to view the encoding of the client system environment:
> chcp
Active code page: 936
The code page 936
shown above is an alias for the GBK
encoding.
You can view the database client encoding as follows:
postgres=# SHOW client_encoding;
client_encoding
-----------------
GBK
(1 row)
This converts the text data from client-side encoding to database server-side encoding.
You can view the current server encoding like this:
postgres=# SHOW server_encoding;
server_encoding
-----------------
UTF8
(1 row)
If the encoding parameter of the database client does not match the encoding of the client system environment, you can modify the database client encoding as follows:
SET client_encoding = 'UTF8'
If the encoding of the SQL script file does not match the encoding parameter of the database client, you can modify the encoding of the SQL script file as follows:
$ iconv --from-code=GBK --to-code=UTF-8 test-gbk.sql > test-utf8.sql