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

时间:2013-01-25 16:01:34

标签: postgresql encoding latin1

我们的postgres生产数据库服务器有一个名为crd_production的数据库,它出自template1模板数据库。顺便提一下,在Ubuntu 12.04框中,初始创建pgcluster时template1和template0数据库的默认编码的默认编码为LATIN1。我删除了template1 db并使用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)

我们最终部署了rails(3.2.11)app并开始使用crd_production db作为主数据库。 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" 

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

2 个答案:

答案 0 :(得分:51)

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

crd_production=# show client_encoding;
 client_encoding 
-----------------
 LATIN1
(1 row)

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

crd_production=#  SET client_encoding = 'UTF8';
SET

再次检查

crd_production=# show client_encoding;
 client_encoding 
-----------------
 UTF8
(1 row)

现在情况正常。

答案 1 :(得分:2)

我以前在postgresql 10的Rails上使用过ruby,这是同样的情况。

update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = 'thedb'

来源:How do you change the character encoding of a postgres database?

相关问题