【解決方法】:
方法一:設置postgresql的客戶端編碼為GBK,這時PostgreSQL就知道輸入的內容是GBK編碼的,這樣PostgreSQL數據庫會自動做字符集的轉換,把其轉換成UTF8編碼。
方法二:直接設置終端的字符集編碼為UTF8,讓輸入的編碼直接為UTF8,而不是GBK。
【具體演示】:
設置postgresql的客戶端編碼:
設置psql客戶端字符集為GBK,方法有兩種,一種是在psql中輸入“\encoding GBK” ,另一種是設置環境變量“export PGCLIENTENCODING=GBK”,演示如下:
#psql -d dsc
dsc=# insert into t values(1,'中國');
ERROR: invalid byte sequence for encoding "UTF8": 0xd6d0
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
dsc=# show client_encoding;
client_encoding
-----------------
UTF8
(1 row)
dsc=# \encoding GBK
dsc=# show client_encoding;
client_encoding
-----------------
GBK
(1 row)
dsc=# insert into t values(1,'中國');
INSERT 0 1
dsc=# commit;
WARNING: there is no transaction in progress
COMMIT
dsc=# select * from t;
id | name
----+------
1 | 中國
(1 row)
[postgres@dsc ~]$ export PGCLIENTENCODING=GBK
[postgres@dsc ~]$ psql
psql: FATAL: conversion between GBK and LATIN1 is not supported
[postgres@dsc ~]$ psql -d dsc
psql (8.4.3)
Type "help" for help.
dsc=# select * from t;
id | name
----+------
1 | 中國
(1 row)
dsc=# insert into t values(2,'我的中國');
INSERT 0 1
dsc=# select * from t;
id | name
----+----------
1 | 中國
2 | 我的中國
(2 rows)