今天去创建新环境装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个模板库到底是有什么用? 有待以后研究。
如果路过的各位有了解的,请多多赐教。