我正在尝试更改我正在运行的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)
答案 0 :(得分:9)
好的,首先要做的事情是:为什么不设置用户或数据库编码有效?
原来这是因为来自the psql documentation的这一行:
如果标准输入或标准输出中的至少一个是终端,则psql将客户端编码设置为“auto”,这将从区域设置(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执行一个命令字符串,命令,然后退出。这在shell脚本中很有用。 此选项会忽略启动文件(
psqlrc
和~/.psqlrc
)。
答案 1 :(得分:1)
您是否在client_encoding
中设置postgresql.conf
(并重新加载配置或重启)?确保它的UTF8不是utf8
cat ~/.psqlrc
和cat /etc/psqlrc
的结果是什么?
我知道您正在寻找服务器端默认设置,但在客户端上,您可以设置操作系统envvar:
export PGCLIENTENCODING=UTF8
为所有用户(在该计算机上)执行此操作,将其放在/etc/profile