当初学者在使用PostgreSQL数据库,输入中文时,会遇到“ERROR: invalid byte sequence for encoding "UTF8": 0xd6d0”的错误,原因是由于没有正确设置客户端字符集。
问题的原因:
默认情况下,PostgreSQL是不转换字符集的,如果你的数据库是UTF8的字符集,一般终端的中文字符集会设置为GBK,或en_US(查看终端的字符集可以看LANG环境变量的设置),所以你输入的中文是GBK的编码,这个编码不经转换的存入数据库中,而数据库是UTF8的,PostgreSQL一看没有这样的UTF8编码,所以当然报错了。
解决方法为:
方法一:设置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)
#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=# 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)
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)
id | name
----+------
1 | 中国
(1 row)
dsc=# insert into t values(2,'我的中国');
INSERT 0 1
dsc=# select * from t;
id | name
----+----------
1 | 中国
2 | 我的中国
(2 rows)
INSERT 0 1
dsc=# select * from t;
id | name
----+----------
1 | 中国
2 | 我的中国
(2 rows)
方法二:设置终端的编码为UTF8:
[postgres@dsc ~]$ export LANG=zh_CN.UTF8
然后修改终端软件的字符集编码,我使用的是SecureCRT,修改方法为:
Option->Session Option->外观->字符编码,把那个下拉框的内容改成“UTF8”:

然后再插入数据测试:
[postgres@dsc ~]$ psql -d dsc
psql (8.4.3)
Type "help" for help.
dsc=# select * from t;
id | name
----+----------
1 | 中国
2 | 我的中国
(2 rows)
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)
INSERT 0 1
dsc=# select * from t;
id | name
----+----------
1 | 中国
2 | 我的中国
3 | 我的中国
(3 rows)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2015-08-11 Linux系统下UDP发送和接收广播消息小例子
2014-08-11 boost 循环缓冲区
2014-08-11 boost::property_tree读取解析.xml文件
2014-08-11 boost::property_tree读取解析ini文件--推荐
2014-08-11 使用ffmpeg向crtmpserver发布rtsp流
2014-08-11 crtmpserver流媒体服务器的介绍与搭建
2014-08-11 boost中asio网络库多线程并发处理实现,以及asio在多线程模型中线程的调度情况和线程安全。