postgresql - PostgreSQL 的 `initdb` 因“无效的区域设置;检查 LANG 和 LC_* 环境变量”而失败
问题描述
已经找到了解决方案(见下面的答案),但我不确定它是否合适;另外,这也可能对其他人有所帮助。
尝试按照文档( 18.2 创建数据库集群)设置 PostgreSQL ,但在 Ubuntu 18.04(内核:4.15.0-22-generic)上出现以下错误:
$ initdb -D /usr/local/pgsql/data
(...)
initdb: invalid locale settings; check LANG and LC_* environment variables
在 Stackoverflow ( 1 , 2 )上找到了几个相关的答案,但这些并没有解决问题,Serverfault 上的一个建议重新启动服务,但 PostgreSQL 甚至没有运行。
尝试在我在系统上找到的每个变体中显式传递语言环境,但这些也失败了,
3617 2018/06/07-08:36 initdb -D ~/Downloads/ --locale=en_US.utf8
3618 2018/06/07-08:36 initdb -D ~/Downloads/ --locale=en_US.UTF8
3621 2018/06/07-08:37 initdb -D ~/Downloads/ --locale=en_US.UTF-8
3622 2018/06/07-08:37 initdb -D ~/Downloads/ --locale="en_US.UTF-8"
3623 2018/06/07-08:37 initdb -D ~/Downloads/ --locale="en_US.utf8"
3645 2018/06/07-09:24 initdb -D ~/Downloads/ --locale="en_US.utf8"
和
initdb: invalid locale name <the_option_value_above>
Arch Linux 论坛对此进行了讨论,但没有解决方案。
2018/06/07 1214 更新
我在上面链接了答案,但可能不够明确:我确实看过locale -a
and locale
(没有列出前者的输出,因为我在下面的尝试中安装了所有这些):
$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
什么已经尝试过,但没有奏效(每次迭代都重新启动终端):
https://askubuntu.com/questions/454260/how-to-solve-locale-problem
选择并配置所有语言环境。
$ sudo locale-gen en_US.UTF-8 $ sudo dpkg-reconfigure locales.
https://github.com/singularityware/singularity/issues/11
两者都不
echo "LC_ALL=en_US.UTF-8" >> /etc/environment echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen echo "LANG=en_US.UTF-8" > /etc/locale.conf locale-gen en_US.UTF-8
也不
sudo apt clean sudo apt update sudo apt upgrade sudo apt-get install -y locales language-pack-fi language-pack-en export LANGUAGE=en_US.UTF-8 export LANG=en_US.UTF-8 export LC_ALL=en_US.UTF-8 sudo locale-gen en_US.UTF-8 sudo dpkg-reconfigure locales
https://askubuntu.com/questions/162391/how-do-i-fix-my-locale-issue/229512#229512
https://askubuntu.com/questions/114759/warning-setlocale-lc-all-cannot-change-locale
(基本上是上面 github 链接的变体,无论如何都试过了。)
解决方案
从这个线程:
initdb -D <your_data_location> --no-locale --encoding=UTF8
在哪里
--locale=LOCALE set default locale for new databases
--no-locale equivalent to --locale=C
有一些注意事项(请参阅下面的警告),但可以使用创建全 utf8 数据库template0
(请参阅21.3. 模板数据库)。
来自客户端(psql
):
postgres=# create database test LC_COLLATE "en_US.UTF-8" LC_CTYPE "en_US.UTF-8" template template0;
或通过createdb
:
createdb --lc-collate="en_US.UTF-8" --lc-ctype="en_US.UTF-8" --template="template0" test2
查看:
$ psql
psql (10.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
test2 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
警告:这可能不是正确的解决方案,上面的解决方法就是一种解决方法。
请注意使用上述解决方案创建的数据库中下面的“Collate”和“Ctype”字段,这可能会导致问题,因为“字符串之间的比较结果取决于LC_CTYPE
。实际上,最明显的效果是排序顺序。” (请参阅 DBA StackExchange 线程)。这在 PostgreSQL 邮件列表中也得到了证实(请参阅有关生产数据库中此问题的线程)。解决此问题的最简单方法可能是重新初始化/重新创建数据库。
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)