【问题标题】:Postgres issue encoding "UTF8" has no equivalent in encoding "LATIN1"Postgres 问题编码“UTF8”在编码“LATIN1”中没有等价物
【发布时间】:2013-01-09 15:14:43
【问题描述】:

我们的 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) 应用程序并开始使用 crd_production 数据库作为主数据库。 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" 

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

【问题讨论】:

    标签: postgresql encoding latin1


    【解决方案1】:

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

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

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

    【讨论】:

    • 值得注意的是,这种方法实际上改变了数据库的编码,而不仅仅是您的(本地?)客户端。这可能会给使用数据库并假设另一种编码的其他人带来问题!除非您确定这没问题,否则请按照 papdel 的回答建议更改客户端。
    【解决方案2】:

    正如猜测的那样,问题出在数据库上的 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)
    

    现在一切正常。

    【讨论】:

    • 看看你能不能做一个alter user set client_encoding='UTF8';从现在开始让它变得粘稠。或者,如果您愿意,也可以更改数据库。
    • 我遇到了同样的错误,但是当我尝试检查 client_encoding 时,它显示的是 UNICODE 而不是 LATIN1。嗯。
    • @ScottMarlowe 仅供参考,这似乎对我不起作用:stackoverflow.com/q/36922248/1157054
    • 嗨,我对这个client_encoding和server_encoding有点困惑,谁能解释一下区别?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多