今天去創建新環境裝PG 並做2臺PG庫的主從同步。
裝完庫,發現庫的默認字符集變成LATIN1. (因爲自己偷懶,創建庫的時候沒有指定encoding)頓時感覺無語。因爲最近各種庫的莫名其妙亂碼,中文亂碼等N中亂碼搞的難受。最後靜下來 ,理了理思路。
1 . 沒有去檢查 /etc/profile 文件。應該在裏面加入:
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
整體的環境變量如果設置好了也不會出現後面的事。postgres用戶也不會變成別的字符集。
2.. postgres 到現在還沒有什麼語句能像ORACLE一樣能直接轉換字符集的命令。
所以我們能想到的僅僅是把數據導出 ,新建庫,然後再導入。這裏導入導出就不多說了。
附一個建庫的語句:
create database test with encoding = 'UTF8' LC_CTYPE = 'en_US.UTF-8' LC_COLLATE = 'en_US.UTF-8' template = template1;
3 .好了 事情又來了 ,報錯
ERROR: new encoding (utf8) is incompatible with the encoding of the template database.
原來我這個不僅創建的數據庫字符集是LATIN1 ,連postgres和template 0 和template1 都是LATIN1.
下面是解決代碼:
update pg_database set datallowconn = TRUE where datname = 'template0';
\c template0
update pg_database set datistemplate = FALSE where datname = 'template1';
drop database template1;
create database template1 with encoding = 'UTF8' LC_CTYPE = 'en_US.UTF-8' LC_COLLATE = 'en_US.UTF-8' template = template0;
update pg_database set datallowconn = TRUE where datname = 'template1';
\c template1
update pg_database set datallowconn = FALSE where datname = 'template0';
至於pg庫默認的template0 和template1 2個模板庫到底是有什麼用? 有待以後研究。
如果路過的各位有了解的,請多多賜教。