【问题标题】:PostgreSQL's `initdb` fails with "invalid locale settings; check LANG and LC_* environment variables"PostgreSQL 的 `initdb` 因“无效的区域设置;检查 LANG 和 LC_* 环境变量”而失败
【发布时间】:2018-11-17 15:49:18
【问题描述】:

已经找到了解决方案(see answer below),但我不确定它是否合适;另外,这也可能对其他人有所帮助。


尝试按照文档 (18.2 Creating a Database Cluster) 设置 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)上找到了一些相关的答案,但这些都没有解决问题,one on 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>

有an Arch Linux forum discussion about this,但没有解决办法。


2018/06/07 1214 更新

我在上面链接了答案,但可能不够明确:我确实看过 locale -a 和 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://unix.stackexchange.com/questions/294845/bash-warning-setlocale-lc-all-cannot-change-locale-en-us-utf-8

【问题讨论】:

    标签: postgresql locale


    【解决方案1】:

    虽然问题没有提到 Nix,但原发帖人从Nix discourse site 链接到这个问题,所以我认为这是一个与 Nix 相关的问题。

    我在 Nix shell 下运行时遇到了这个问题,经过大量搜索找到了解决方案here。我只需要将glibcLocales 添加到我的环境中。 IE。运行nix-shell -p glibcLocales 或将glibcLocales 添加到buildInputs。

    【讨论】:

    • 谢谢!是的,我最初在 NixOS 上遇到了这个问题,但后来在较旧的 Ubuntu 上也遇到了这个问题,因此有必要提出一个一般性问题。我现在不能尝试这个,但非常感谢你的努力 - 这可能是一个很难找到解决方案的问题..
    【解决方案2】:
    1. 检查是否在/etc/locale.gen 中启用了区域设置。在我全新安装的 Arch Linux ARM 中,以下行被注释掉了:
      en_US.UTF-8 UTF-8
      
    2. 不带任何参数运行locale-gen。它将在生成时列出所有未注释的语言环境。
    3. 可选:编辑/etc/locale.conf 以设置系统区域设置:
      echo "LANG=en_US.UTF-8" > /etc/locale.conf
      
      重新启动系统以使所有服务都采用新设置。
    4. 运行initdb 命令。

    【讨论】:

      【解决方案3】:

      您可以使用以下命令获取 Linux 中可用的语言环境列表

      locale -a
      

      使用其中之一。

      您必须选择与您的编码相匹配的语言环境,例如

      initdb -E UTF8 --locale=en_US.utf8
      

      或

      initdb -E LATIN9 --locale=et_EE.iso885915
      

      据我所知,您可以安装其他语言环境

      sudo apt-get install language-pack-XX
      

      【讨论】:

      【解决方案4】:

      来自this thread:

      initdb -D <your_data_location> --no-locale --encoding=UTF8
      

      在哪里

        --locale=LOCALE       set default locale for new databases
        --no-locale           equivalent to --locale=C
      

      有一些注意事项(请参阅下面的警告),但可以使用 template0 创建全 utf8 数据库(请参阅 21.3. Template Databases)。

      来自客户端 (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 | 
      

      警告:这可能不是正确的解决方案,上面的解决方法就是一种解决方法。

      注意使用上述解决方案创建的数据库中的“Collat​​e”和“Ctype”字段,这可能会导致问题,因为“字符串之间的比较结果取决于LC_CTYPE。实际上,最可见效果是排序顺序。" (see DBA StackExchange thread)。这也在 PostgreSQL 邮件列表中得到确认(请参阅this thread 关于生产数据库中的此问题)。解决这个问题的最简单方法可能是re-initializing/recreating the database。

      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)
      

      【讨论】:

        猜你喜欢
        • 2017-06-16
        • 2014-12-07
        • 2015-02-07
        • 1970-01-01
        • 1970-01-01
        • 2021-02-15
        • 2019-03-16
        • 1970-01-01
        • 2013-10-30
        相关资源
        最近更新 更多