溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

postgresql插入漢字報錯

發(fā)布時間:2020-05-21 16:40:23 來源:網絡 閱讀:697 作者:斷臂人 欄目:數(shù)據(jù)庫

錯誤信息: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
思路:因為沒有修改環(huán)境變量,導致安裝完數(shù)據(jù)庫,數(shù)據(jù)庫默認字符集變成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)
所以如果一開始修改了環(huán)境變量,也不會出現(xiàn)之后的問題。修改環(huán)境變量# vi /etc/profileexport.UTF-8export LC_ALL=en_US.UTF-8export LC_CTYPE=en_US.UTF-8
# source /etc/profile
現(xiàn)在數(shù)據(jù)庫已經安裝,解決辦法只能是在建庫的時候指定字符集。
首先修改環(huán)境變量
命令參考: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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI