【发布时间】:2023-02-11 04:46:56
【问题描述】:
在 Ubuntu 上从 apt 安装 PostgreSQL 时,initdb 初始化集群的命令是自动完成的,the locale is set from the enviroment.
我喜欢在en_US.UTF8 中安装我的系统,但在不同的语言环境中初始化集群。
为此,我尝试在本地为 apt 设置环境变量
LOCALE=es_ES.UTF-8 LC_MESSAGES=C apt install postgresql-15
但它不工作。 apt输出显示:
/usr/lib/postgresql/15/bin/initdb -D /var/lib/postgresql/15/main --auth-local peer --auth-host scram-sha-256 --no-instructions
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
为了完成,我的配置脚本的简化版本如下所示:
MY_LOCALE="es_ES.UTF-8"
# Allow the system to use $MY_LOCALE
sed -i "s/^# ${MY_LOCALE} UTF-8/${MY_LOCALE} UTF-8/" /etc/locale.gen
locale-gen
# Uncomment these lines if $MY_LOCALE must be the default
# update-locale --reset LANG="${MY_LOCALE}" LC_CTYPE="${MY_LOCALE}"
# export LANG="${MY_LOCALE}"
# export LC_TYPE="${MY_LOCALE}"
# Set the locale locally to apt command
LOCALE="${MY_LOCALE}" LC_MESSAGES=C apt install postgresql-15
有什么方法可以在安装时将语言环境变量传递给 apt/initdb 吗?
更新。
由于问题已关闭且无法添加答案,我编辑以解释我的实际解决方法:
MY_LOCALE="es_ES.UTF-8"
# Allow the system to use $MY_LOCALE
sed -i "s/^# ${MY_LOCALE} UTF-8/${MY_LOCALE} UTF-8/" /etc/locale.gen
locale-gen
# Uncomment these lines if $MY_LOCALE must be the default
# update-locale --reset LANG="${MY_LOCALE}" LC_CTYPE="${MY_LOCALE}"
# export LANG="${MY_LOCALE}"
# export LC_TYPE="${MY_LOCALE}"
# Backup default enviroment variables
BCK_LANG="${LANG}"
BCK_LC_CTYPE="${LC_CTYPE}"
BCK_LC_MESSAGES="${LC_MESSAGES}"
# Set the desired locale for PostgreSQL as default for the system
update-locale --reset LANG="${MY_LOCALE}" LC_CTYPE="${MY_LOCALE}" LC_MESSAGES=C
# Install PostgreSQL
apt install postgresql-15
# Restore default locale
update-locale --reset LANG="${BCK_LANG}" LC_CTYPE="${BCK_LC_CTYPE}" LC_MESSAGES="${BCK_LC_MESSAGES}"
unset BCK_LANG
unset BCK_LC_CTYPE
unset BCK_LC_MESSAGES
【问题讨论】:
-
也许你设置了
LC_ALL?这将覆盖LC_xyz和LANG:将按此顺序查询以下环境变量,直到找到一个已设置:LC_ALL、LC_COLLATE(或对应于相应类别的变量)、LANG。如果没有设置这些环境变量,则语言环境默认为 C。 -
感谢您的提示,但未设置
LC_ALL。
标签: postgresql ubuntu locale