You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下安装PostgreSQL时如何指定集群Locale?

在Ubuntu上为PostgreSQL安装指定初始化Locale的问题

在Ubuntu上通过apt安装PostgreSQL时,初始化集群的initdb命令会自动执行,数据库集群的Locale由环境变量决定。

我希望系统Locale保持为en_US.UTF8,但用不同的Locale初始化数据库集群。

为此,我尝试给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"

# 允许系统使用指定Locale
sed -i "s/^# ${MY_LOCALE} UTF-8/${MY_LOCALE} UTF-8/" /etc/locale.gen
locale-gen

# 如果需要将指定Locale设为系统默认,取消以下注释
# update-locale --reset LANG="${MY_LOCALE}" LC_CTYPE="${MY_LOCALE}"
# export LANG="${MY_LOCALE}"
# export LC_TYPE="${MY_LOCALE}"

# 给apt命令临时设置Locale
LOCALE="${MY_LOCALE}" LC_MESSAGES=C apt install postgresql-15

请问有没有办法在安装时将Locale变量传递给apt/initdb?


更新:
由于问题已关闭无法添加答案,我在此补充实际使用的临时解决方法:

MY_LOCALE="es_ES.UTF-8"

# 允许系统使用指定Locale
sed -i "s/^# ${MY_LOCALE} UTF-8/${MY_LOCALE} UTF-8/" /etc/locale.gen
locale-gen

# 如果需要将指定Locale设为系统默认,取消以下注释
# update-locale --reset LANG="${MY_LOCALE}" LC_CTYPE="${MY_LOCALE}"
# export LANG="${MY_LOCALE}"
# export LC_TYPE="${MY_LOCALE}"

# 备份默认环境变量
BCK_LANG="${LANG}"
BCK_LC_CTYPE="${LC_CTYPE}"
BCK_LC_MESSAGES="${LC_MESSAGES}"

# 将PostgreSQL需要的Locale设为系统默认
update-locale --reset LANG="${MY_LOCALE}" LC_CTYPE="${MY_LOCALE}" LC_MESSAGES=C

# 安装PostgreSQL
apt install postgresql-15

# 恢复系统默认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

内容的提问来源于stack exchange,提问作者Francisco Puga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 09:05:38