作者:朱金灿
来源:clever101的专栏
系统环境是Windows 11 专业版,PostgreSQL版本是17。在运行sql语句创建数据库时出现错误:
閿欒: template database \"template1\" has a collation version mismatch
DETAIL: The template database was created using collation version 1540.3,1540.3, but the operating system provides version 1541
经过搜索,发现根源在于:在 Windows 11 上遇到PostgreSQL的 template1 数据库 collation 版本不匹配问题时,通常是由于系统更新导致操作系统 collation 版本升级,而 PostgreSQL 的模板数据库未同步更新。
要解决这个问题,需要更新 template1的collation。具体做法如下:
1.备份数据
#停止PostgreSQL服务,这个的<version>为PostgreSQL的主版本号,比如我的是17
net stop postgresql-x64-<version># 备份数据目录(如 C:\Program Files\PostgreSQL\<version>\data)
xcopy "D:\Program Files\PostgreSQL\17\data" "D:\TestData\postgres_data" /E /H /C /I
2.重新初始化数据库集群
# 使用 initdb 重新初始化(确保路径与安装配置一致)
"D:\Program Files\PostgreSQL\17\bin\initdb.exe" -D "D:\Program Files\PostgreSQL\17\data" -U postgres -A password -W --encoding=UTF8 --lc-collate=C --lc-ctype=C
3.重启 PostgreSQL 服务
#启动PostgreSQL服务,这个的<version>为PostgreSQL的主版本号,比如我的是17
net start postgresql-x64-<version>