postgresql插入汉字报错
错误信息: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 = 'tem
5ae
plate1';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://www.geek-share.com/detail/2687436043.html
- MySql插入汉字时报错Incorrect string value
- 【随笔】postgresql导出sql插入语句
- Python随机生成数据后插入到PostgreSQL
- PostgreSQL 黑科技-递规二分法切分汉字
- Postgresql 之 优化数据的批量插入
- 代码里汉字插入数据库变成??
- thinkphp插入数据时,汉字为空,数字及英文不为空,是因为模板不是utf8编码
- MySQL插入显示汉字出现乱码问题的解决
- postgresql插入后返回id
- sql2005插入汉字后乱码解決方法
- postgresql 插入字符串有空格,见图片
- 插入到Mysql数据库中的汉字乱码
- postgresql修改插入数据id的起始值
- mysql插入汉字是乱码或者问号的解决方法
- 在Postgresql表中默认插入当前时间
- 完美解决SQL server2005中插入汉字变成问号的问题
- java中插入汉字到mysql中变成?的处理办法
- PostgreSQL数据库日常学习笔记2-插入数据
- oracle 插入汉字时字符编码问题
- postgresql数据库中geometry类型的字段插入经纬度脚本