PostgreSQL字符集问题
转自:osdba的文章《初学者遇到的PostgreSQL字符集问题的解决》 当初学者在使用PostgreSQL数据库,输入中文时,会遇到“ERROR: invalid byte sequence for encoding “UTF8”: 0xd6d0”的错误,原因是由于没有正确设置客户端字符集。 问题的原因: 看我具体的演示: 方法一:设置postgresql的客户端编码: #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)
方法二:设置终端的编码为UTF8: [postgres@dsc ~]$ export LANG=zh_CN.UTF8 然后修改终端软件的字符集编码,我使用的是SecureCRT,修改方法为: 然后再插入数据测试: [postgres@dsc ~]$ psql -d dsc
psql (8.4.3)
Type "help" for help.
dsc=# select * from t;
id | name ----+----------
1 | 中国
2 | 我的中国
(2 rows)
dsc=# insert into t values(3,'我的中国');
INSERT 0 1
dsc=# select * from t;
id | name ----+----------
1 | 中国
2 | 我的中国
3 | 我的中国
(3 rows)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |