编码"UTF8"的Postgres问题在编码"LATIN1"时没有等价物

pap*_*del 20 postgresql encoding latin1

我们的postgres生产数据库服务器有一个名为crd_production的数据库,它出自template1模板数据库.顺便提一下,在Ubuntu 12.04框中,初始创建pgcluster时template1和template0数据库的默认编码的默认编码为LATIN1.我删除了template1数据库并使用utf-8编码重新创建它,如下所示.

      Name      |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
----------------+----------+----------+------------+------------+-----------------------
 crd_production | deployer | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres       | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0      | postgres | LATIN1   | en_US      | en_US      | =c/postgres          +
                |          |          |            |            | postgres=CTc/postgres
 template1      | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
(4 rows)
Run Code Online (Sandbox Code Playgroud)

我们最终部署了rails(3.2.11)app并开始使用crd_productiondb作为主数据库.ActiveRecord写入/读取数据时没有问题,但是当我尝试从此psql数据库上的命令行触发任何sql查询时,会发生以下错误 -

crd_production=# select * from users;
ERROR:  character with byte sequence 0xe2 0x80 0x9c in encoding "UTF8" has no equivalent in encoding "LATIN1" 

crd_production=# select * from features;
ERROR:  character with byte sequence 0xe2 0x80 0x99 in encoding "UTF8" has no equivalent in encoding "LATIN1" 
Run Code Online (Sandbox Code Playgroud)

这可能是什么问题?这是客户的问题吗?

pap*_*del 51

正如猜测的那样,问题在于数据库上的client_encoding.

crd_production=# show client_encoding;
 client_encoding 
-----------------
 LATIN1
(1 row)
Run Code Online (Sandbox Code Playgroud)

要将客户端编码更改为UTF-8,您需要执行此操作

crd_production=#  SET client_encoding = 'UTF8';
SET
Run Code Online (Sandbox Code Playgroud)

再检查一遍

crd_production=# show client_encoding;
 client_encoding 
-----------------
 UTF8
(1 row)
Run Code Online (Sandbox Code Playgroud)

事情现在很好.

  • 看看你是否可以做一个alter user set client_encoding ='UTF8'; 从现在开始使这个粘.或者如果您愿意,还可以使用alter database. (2认同)
  • 你好,我对 client_encoding 和 server_encoding 有点困惑,有人能解释一下区别吗? (2认同)

Mad*_*mah 8

我之前在 postgresql 10 上使用 ruby​​ on rails 也有过同样的情况。这就是窍门

update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = 'thedb'
Run Code Online (Sandbox Code Playgroud)

来源:如何更改 postgres 数据库的字符编码?