Invalid byte sequence for encoding "UTF8": 0xe99d2c, utf80xe99d2c
Query failed: ERROR: invalid byte sequence for encoding "UTF8": 0xe99d2c
The reason is that the client character set does not match the character set of the inserted content. PostgreSQL does not perform Character Set conversion by default. If the database is a UTF8 character set, the Chinese Character Set of the terminal is generally set to GBK (check the LANG environment variable for confirmation ), therefore, this encoding is stored in the database without conversion, and the database is UTF8. PostgreSQL reports the following error if it finds that it is not UTF8 encoding.
To enable the automatic character set conversion function, you must inform the PostgreSQL client of the character set used. At this time, you can set the PostgreSQL client encoding to GBK, and pg will automatically perform Character Set conversion.
The following is an experiment:
1
[Root @ hostalonetest ~] # Psql-h 192.168.18.210-Upostgres beiigang
Psql. bin (9.3.5, server 9.1.14)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
Beiigang = #
2
Beiigang = # \ l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------- + ---------- + ------------- + -----------------------
Beiigang | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
Postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
3
Beiigang = # show server_encoding;
Server_encoding
-----------------
UTF8
(1 row)
4
Beiigang = # show client_encoding;
Client_encoding
-----------------
UTF8
(1 row)
5
Beiigang = # create table tb_tt (id int, ctnr varchar (60 ));
CREATE TABLE
6
Beiigang = # insert into tb_tt (id, ctnr) values (1, 'newline ');
ERROR: invalid byte sequence for encoding "UTF8": 0xd0c2
7
Beiigang = # \ encoding GBK
8
Beiigang = # show client_encoding;
Client_encoding
-----------------
GBK
(1 row)
9
Beiigang = # insert into tb_tt (id, ctnr) values (1, 'newline ');
INSERT 0 1
10
Beiigang = # select * from tb_tt;
Id | ctnr
---- + --------
1 | new website
(1 row)
11
Beiigang = # show client_encoding;
Client_encoding
-----------------
GBK
(1 row)
Beiigang = #
Beiigang = # reset client_encoding;
RESET
Beiigang = # show client_encoding;
Client_encoding
-----------------
UTF8
(1 row)
When writing code, you can specify the client character set encoding when creating a database link.
Refer:
Http://www.postgresql.org/docs/9.3/interactive/multibyte.html
-----------------
Please refer to the following source for reprinting:
Blog.csdn.net/beiigang