postgresql插入汉字报错
admin
2023-02-09 02:00:07
0

错误信息:peimsmdb=# select '我';ERROR:  character with byte sequence 0xe6 0x88 0x91 in encoding "UTF8" has no equivalent in encoding "LATIN1"
查看客户端字符集:peimsmdb=# show client_encoding; client_encoding ----------------- UTF8
peimsmdb=# show server_encoding; server_encoding ----------------- LATIN1
思路:因为没有修改环境变量,导致安装完数据库,数据库默认字符集变成LATIN1,如下所示。
postgres=# \l                             List of databases   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   -----------+----------+----------+---------+-------+----------------------- peimsmdb  | peimsmer | LATIN1   | en_US   | en_US | =Tc/peimsmer         +           |          |          |         |       | peimsmer=CTc/peimsmer postgres  | postgres | LATIN1   | en_US   | en_US |  template0 | postgres | LATIN1   | en_US   | en_US | =c/postgres          +           |          |          |         |       | postgres=CTc/postgres template1 | postgres | LATIN1   | en_US   | en_US | =c/postgres          +           |          |          |         |       | postgres=CTc/postgres(4 rows)
所以如果一开始修改了环境变量,也不会出现之后的问题。修改环境变量# vi /etc/profileexport.UTF-8export LC_ALL=en_US.UTF-8export LC_CTYPE=en_US.UTF-8
# source /etc/profile
现在数据库已经安装,解决办法只能是在建库的时候指定字符集。
首先修改环境变量
命令参考:create database DATABASENAME with encoding = 'UTF8' LC_CTYPE = 'en_US.UTF-8' LC_COLLATE = 'en_US.UTF-8' template = template1;
在建库之前首先处理template0和template1两个表。
# psql -U postgres -d postgres -h 127.0.0.1
postgres=# update pg_database set datallowconn = TRUE where datname = 'template0';UPDATE 1postgres=# \c template0You are now connected to database "template0" as user "postgres".template0=# update pg_database set datistemplate = FALSE where datname = 'template1';UPDATE 1template0=# drop database template1;DROP DATABASEtemplate0=# create database template1 with encoding = 'UTF8' LC_CTYPE = 'en_US.UTF-8' LC_COLLATE = 'en_US.UTF-8' template = template0;CREATE DATABASEtemplate0=# update pg_database set datallowconn = TRUE where datname = 'template1';UPDATE 1template0=# \c template1You are now connected to database "template1" as user "postgres".template1=# update pg_database set datallowconn = FALSE where datname = 'template0';UPDATE 1参考:https://blog.csdn.net/hkyw000/article/details/52817422

相关内容

热门资讯

王虹、邓煜为啥能获菲尔兹奖? 当地时间7月23日早晨,2026年国际数学家大会在美国费城开幕。国际数学联盟在开幕式上正式公布第二十...
高温津贴,不能用绿豆汤冲抵 近日,深圳市人力资源和社会保障局发布关于高温津贴的典型劳动仲裁案例,并普法提醒:即便劳动合同约定日薪...
菲律宾,你到底想干什么 中国和菲律宾关系,又出幺蛾子了。反正,最近几天,麻烦不断,场面火爆。仁爱礁,菲两艘小艇冲撞我巡逻船只...
聚焦长期价值,方舟云康以数智化... 来源:江南时报 在慢病管理领域,单纯追逐“流量”的互联网医疗模式已显疲态。当线上获客成本持续走高、用...
阿拉比卡以后喝不到了?对话云大... 搜狐科技《思想大爆炸——对话科学家》栏目第157期,对话云南大学农学院教授何飞飞。 嘉宾简介: 何飞...
微信重磅更新!超实用 7月23日,微信派发文晒微信未成年人模式,进入“未成年人模式”后,家长可为孩子设置“每日消费限额”和...
13岁少年帮聋哑舅舅卖瓜,记者... 河南13岁少年帮聋哑舅舅卖瓜走红,记者购买2万斤西瓜助农,临走时少年妈妈拉着记者表示感谢:“如果不是...
12999元起!三星首款阔折叠... 7月22日晚,三星举行 Galaxy Z8 系列折叠屏手机全球发布会,一口气带来了Galaxy Z ...
西门子抽油烟机为什么不转了 西门子抽油烟机为什么不转了1、可能是油烟机里面的电机坏掉了,或者是里面的风扇叶片上有太多的油渍,也有...
四预警齐发!台风“红霞”最新路... 今天(24日)6时,中央气象台发布高温黄色预警、暴雨蓝色预警、强对流天气蓝色预警、台风橙色预警。高温...