2016-04-28 101 views
3

我試圖改變我運行的PostgreSQL數據庫的client_encoding配置變量的默認值。我希望它是UTF8,但目前它設置爲LATIN1如何更改Postgres中的默認client_encoding?

數據庫已被設置爲使用UTF-8編碼:

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文件:

[email protected]:~$ cat ~/.psqlrc 
cat: /home/vagrant/.psqlrc: No such file or directory 
[email protected]:~$ cat /etc/psqlrc 
cat: /etc/psqlrc: No such file or directory 
[email protected]:~$ sudo su 
[email protected]:/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) 

回答

6

好吧,首先要做的事情是:爲什麼不設置用戶或數據庫編碼有什麼作用?

原來這是因爲這條線從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將執行一條命令字符串,命令,然後退出。這在shell腳本中很有用。 啓動文件(psqlrc~/.psqlrc)被忽略此選項。

1

你設置postgresql.confclient_encoding(並重新加載配置或重新啓動)?確保它的UTF8不是utf8

cat ~/.psqlrccat /etc/psqlrc的結果是什麼?

我知道你正在尋找服務器端的默認,但在客戶端上,你可以設置一個OS ENVVAR:

export PGCLIENTENCODING=UTF8

爲所有用戶做到這一點(那臺機器上),把在/etc/profile

+0

「cat〜/ .psqlrc和cat/etc/psqlrc的結果是什麼?」添加到問題 – Ajedi32

+0

將'client_encoding ='UTF8''附加到'/ etc/postgresql/9.1/main/postgresql.conf'並使用'/etc/init.d/postgresql restart'重新啓動Postgres不起作用。新連接的客戶端編碼仍然是'LATIN1'。設置環境變量確實有效,但就像你說的那樣是客戶特定的。雖然我的情況可能會「足夠好」...... – Ajedi32