Postgres 数据库字符集更改 ERROR: new encoding (UTF8) is incompatible

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方-phantom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值