PostgresQL 创建数据库报错:ERROR: template database "template1" has a collation version mismatch
postgres=# CREATE DATABASE hoppscotch;
ERROR: template database "template1" has a collation version mismatch
DETAIL: The template database was created using collation version 2.36, but the operating system provides version 2.41.
HINT: Rebuild all objects in the template database that use the default collation and run ALTER DATABASE template1 REFRESH COLLATION VERSION, or build PostgreSQL with the right library version.
这个错误是由于 PostgreSQL 的 template1 模板数据库的 collation(排序规则)版本 与当前操作系统提供的 glibc(或系统 locale 库)版本不一致导致的。
具体来说:
template1 是在 glibc 2.36 环境下创建的;
- 而你现在运行 PostgreSQL 的系统已经升级到了 glibc 2.41;
- PostgreSQL 在创建新数据库时会复制
template1,但发现其 collation 版本过旧,出于数据一致性考虑拒绝操作。
✅ 解决方案
刷新 template1 的 collation 版本(推荐,如果数据可接受重建)
⚠️ 注意:此操作会重建所有使用默认 collation 的对象(如索引、表等),但不会删除数据。不过仍建议先备份重要数据!
以超级用户登录 PostgreSQL:
psql -U postgres
执行刷新命令:
ALTER DATABASE template1 REFRESH COLLATION VERSION;
再次尝试创建数据库:
CREATE DATABASE hoppscotch;