作者:朱金燦
來源: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>