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

标签 postgresql

我正在尝试更改 client_encoding 的默认值我正在运行的 PostgreSQL 数据库的配置变量。我希望它是 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.

关于postgresql - 如何更改 Postgres 中的默认 client_encoding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922248/

相关文章:

postgresql - 使任何缓存失效(刷新)的合理时间间隔是多少?

PostgreSQL:将两个时间戳设置为彼此相等。其中之一有与之关联的触发器

hibernate - 如何从 postgresql 数据库获取最新记录?

python - flask-migrate 不检测模型

ruby-on-rails - 在 Postgres 上使用 Rails 的日期时间字段

sql - 如何在 UPDATE 查询中的 SELECT 之前的 FROM 之后使用 CASE 语句

postgresql - search_path 如何影响标识符解析和 "current schema"

sql - 如何在 PostgreSQL 中将表设置为只读

postgresql - postgres 中表继承需要索引吗?

python - 如何将包含外键模型关系的 Django 模型导出到组合文本文件中