加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 18:07:32 所属栏目:百科 来源:网络整理
导读:我正在尝试更改我正在运行的PostgreSQL数据库的client_encoding配置变量的默认值.我希望它是UTF8,但目前它已经设置为LATIN1. 数据库已设置为使用UTF8编码: application_database=# l List of databases Name | Owner | Encoding | Collate | Ctype | Acces
我正在尝试更改我正在运行的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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读