【问题标题】:How do I change the default client_encoding in Postgres?如何更改 Postgres 中的默认 client_encoding?
【发布时间】:2016-04-28 18:01:41
【问题描述】:

我正在尝试更改我正在运行的 PostgreSQL 数据库的 client_encoding 配置变量的默认值。我希望它是UTF8,但目前它被设置为LATIN1

数据库已设置为使用 UTF8 编码:

application_database=# \l
                                                 List of databases
           Name       |  Owner   | Encoding |   Collate   |    Ctype    |          Access privileges
----------------------+----------+----------+-------------+-------------+--------------------------------------
 postgres             | postgres | LATIN1   | en_US       | en_US       |
 application_database | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres           +
                      |          |          |             |             | application_database=Tc/postgres
 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)

哪个according to the docs应该已经导致客户端使用UTF8作为其默认client_encoding(强调我的):

client_encoding (string)

设置客户端编码(字符集)。 默认使用数据库编码。

但它没有:

$ sudo psql --dbname=application_database
psql (9.1.19)
Type "help" for help.

application_database=# SHOW client_encoding;
 client_encoding
-----------------
 LATIN1
(1 row)

我什至尝试使用ALTER USER <user> SET ... 更改我登录用户的默认配置:

application_database=# ALTER USER root SET client_encoding='UTF8';
ALTER ROLE
application_database=# SELECT usename, useconfig FROM pg_shadow;
         usename      |       useconfig
----------------------+------------------------
 postgres             |
 root                 | {client_encoding=UTF8}
 application_database |
(3 rows)

但这也没有效果:

$ sudo psql --dbname=application_database
psql (9.1.19)
Type "help" for help.

application_database=# SELECT current_user;
 current_user
--------------
 root
(1 row)

application_database=# SHOW client_encoding;
 client_encoding
-----------------
 LATIN1
(1 row)

我系统上的任何 PSQL 文件中都没有任何内容:

vagrant@app-database:~$ cat ~/.psqlrc
cat: /home/vagrant/.psqlrc: No such file or directory
vagrant@app-database:~$ cat /etc/psqlrc
cat: /etc/psqlrc: No such file or directory
vagrant@app-database:~$ sudo su
root@app-database:/home/vagrant# cat ~/.psqlrc
cat: /root/.psqlrc: No such file or directory

我正在运行 PosgreSQL 9.1:

application_database=# SELECT version();
                                                   version
-------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.1.19 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 64-bit
(1 row)

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    好的,首先要做的事情是:为什么设置用户或数据库编码没有任何效果?

    原来是因为the psql documentation的这条线:

    如果标准输入或标准输出中至少有一个是终端,则 psql 将客户端编码设置为“自动”,这将从区域设置(Unix 系统上的 LC_CTYPE 环境变量)中检测适当的客户端编码。如果这没有按预期工作,可以使用环境变量 PGCLIENTENCODING 覆盖客户端编码。

    所以,事实上,您之前的配置更改实际上一直在起作用,只是在交互式psql 控制台中没有。试试下面的命令:

    sudo psql --dbname=application_database -c "SHOW client_encoding;" | cat
    

    您应该看到客户端编码实际上是 UTF8:

     client_encoding
    -----------------
     UTF8
    (1 row)
    

    现在再次运行该命令,但不要将其通过管道传递给cat

    sudo psql --dbname=application_database -c "SHOW client_encoding;"
    

    你应该得到结果:

     client_encoding
    -----------------
     LATIN1
    (1 row)
    

    所以基本上,psql 只对涉及终端的命令使用LATIN1 编码。

    你怎么能解决这个问题?嗯,有几种可能的方法。

    一种方法是按照文档的建议将PGCLIENTENCODING 环境变量设置为UTF8 某处持久,例如~/.profile~/.bashrc 仅影响您的用户,或/etc/environment 影响整个系统(见How to permanently set environmental variables)。

    另一种选择是将系统区域设置配置为使用en_US.utf8 而不是en_US(或等效项)。执行此操作的方法可能因您的系统而异,但通常您可以通过修改~/.config/locale.conf(仅限您的用户)或/etc/default/locale/etc/locale.conf(系统范围)来实现。这不仅会影响 postgres,而且我相信更能解决问题的根源。您可以通过运行locale 来检查您当前的区域设置。

    另一种解决方案是更新您的 psqlrc 文件以包含 SET client_encoding=UTF8;。该文件位于~/.psqlrc(仅限您的用户)或/etc/psqlrc(系统范围)。请注意,此方法不会影响我们之前用于客户端编码的命令的结果,因为the docs state(强调我的):

    --command=command

    指定psql是执行一个命令字符串,command,然后退出。这在 shell 脚本中很有用。 使用此选项会忽略启动文件(psqlrc~/.psqlrc)。

    【讨论】:

      【解决方案2】:

      您是否在postgresql.conf 中设置了client_encoding(并重新加载配置或重新启动)?确保它是 UTF8 而不是 utf8

      cat ~/.psqlrccat /etc/psqlrc 的结果是什么?

      我知道您正在寻找服务器端默认值,但在客户端,您可以设置操作系统环境变量:

      export PGCLIENTENCODING=UTF8

      要为所有用户(在该机器上)执行此操作,请将其放入 /etc/profile

      【讨论】:

      • "cat ~/.psqlrc 和 cat /etc/psqlrc 的结果是什么?"添加到问题
      • client_encoding = 'UTF8' 附加到/etc/postgresql/9.1/main/postgresql.conf 并使用/etc/init.d/postgresql restart 重新启动Postgres 无效。新连接的客户端编码仍然是LATIN1。设置环境变量确实有效,但就像你说的那样,这是特定于客户端的。可能对我的情况来说“足够好”......
      猜你喜欢
      • 1970-01-01
      • 2013-09-08
      • 2020-11-14
      • 2011-07-12
      • 2012-09-01
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多