微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 如何更改Postgres中的默认client_encoding?

我正在尝试更改我正在运行的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)

Sets the client-side encoding (character set). The default is to use the database encoding.

但它没有:

$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)
好的,首先要做的事情是:为什么不设置用户数据库编码有效?

事实证明这是因为the psql documentation这条线:

If at least one of standard input or standard output are a terminal,then psql sets the client encoding to “auto”,which will detect the appropriate client encoding from the locale settings (LC_CTYPE environment variable on Unix systems). If this doesn’t work out as expected,the client encoding can be overridden using the environment variable 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

Specifies that psql is to execute one command string,command,and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐