Windows 安装 PostgreSQL 出现 Problem running post-install step. The database cluster initialisation failed. 完整解决方案(超详细排查记录)
适用系统
- Windows 10
- Windows 11
- PostgreSQL 18.x(理论上 17.x、16.x 部分情况也适用)
- EnterpriseDB 官方安装包
前言
最近安装 PostgreSQL 18.4 时,遇到了一个非常奇怪的问题。
安装程序已经执行到最后一步,却突然弹出下面的错误:
text
Problem running post-install step.
Installation may not complete correctly.
The database cluster initialisation failed.
安装程序最后提示:
Installation completed
但是数据库并没有初始化成功,也无法启动 PostgreSQL 服务。
更奇怪的是,日志中出现了一条十分迷惑人的错误:
text
initdb: error:
program "postgres" was found by
"D:/Program Files/PostgreSQL/18/bin/initdb.exe"
but was not the same version as initdb
第一眼看到这个错误,大多数人的第一反应都是:
- PostgreSQL 下载错版本?
- PATH 有多个 PostgreSQL?
- 环境变量冲突?
- DLL 冲突?
- 安装包损坏?
我也是这样认为的。
于是开始了详细排查。
最后发现:
真正原因完全不是 PostgreSQL 版本问题。
希望这篇文章能够帮助遇到同样问题的人少走很多弯路。
我的环境
操作系统:
Windows 10 22H2
版本:
19045.6466
安装包:
postgresql-18.4-2-windows-x64.exe
下载地址:
https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
安装目录:
D:\Program Files\PostgreSQL\18
数据目录:
D:\Program Files\PostgreSQL\18\data
错误现象
安装程序几乎完成时弹出:
text
Problem running post-install step.
Installation may not complete correctly.
The database cluster initialisation failed.
查找安装日志,查看最后几十行的信息。首先,找到该安装日志:
如何找到 PostgreSQL 安装日志
当安装程序提示:
text
Problem running post-install step.
Installation may not complete correctly.
The database cluster initialisation failed.
第一件事应该查看安装日志,而不是盲目重新安装。
方法一:查看临时目录(推荐)
EnterpriseDB 官方安装程序会将安装日志保存在当前用户的临时目录。
通常路径为:
text
C:\Users\<你的用户名>\AppData\Local\Temp\
例如我的安装日志目录:
text
C:\Users\Administrator\AppData\Local\Temp\
进入该目录后,查找:
text
install-postgresql.log
或者直接在资源管理器地址栏输入:
text
%LOCALAPPDATA%\Temp
按 Enter 后即可打开当前用户的临时目录。
也可以在命令提示符中执行:
cmd
echo %TEMP%
或者:
cmd
echo %LOCALAPPDATA%\Temp
例如输出:
text
C:\Users\Administrator\AppData\Local\Temp
然后查看其中是否存在:
text
install-postgresql.log
方法二:安装程序结束后立即查看
如果安装刚刚失败,不要立即关闭所有窗口。
因为安装程序通常会把完整日志写入:
text
install-postgresql.log
日志中会包含类似内容:
text
Initializing PostgreSQL database cluster...
Executing:
initdb.exe ...
initdb exit code = 1
initdb: error:
program "postgres" was found by ...
but was not the same version as initdb
这些信息通常比弹窗提示更有价值,也是定位问题最重要的依据。
方法三:搜索日志文件
如果不知道日志保存在哪里,可以直接在资源管理器搜索:
text
install-postgresql.log
或者在命令提示符执行:
cmd
dir C:\install-postgresql.log /s
也可以搜索整个系统:
cmd
dir install-postgresql.log /s
搜索完成后即可打开对应日志文件。
提示: Windows 搜索整个磁盘可能需要几分钟时间,请耐心等待。
方法四:使用 Everything(推荐)
如果电脑安装了 Everything 搜索工具,可以直接搜索:
text
install-postgresql.log
几乎可以瞬间定位日志所在目录。
我的环境中,安装日志位于:
textC:\Users\Administrator\AppData\Local\Temp\install-postgresql.log不同用户、不同 Windows 版本,路径中的用户名会不同,但通常都位于当前用户的
%LOCALAPPDATA%\Temp目录下。
安装日志中最重要的一段如下:
text
Initializing PostgreSQL database cluster...
Executing:
initdb.exe
...
initdb exit code = 1
initdb:
error:
program "postgres" was found by
"D:/Program Files/PostgreSQL/18/bin/initdb.exe"
but was not the same version as initdb
Failed to initialise the database cluster with initdb
注意这里最关键的一句:
text
program "postgres" was found
but was not the same version as initdb
很多人都会认为:
postgres.exe 和 initdb.exe 版本不一致。
实际上并不是。
第一轮排查
检查 postgres 版本
cmd
postgres --version
输出:
text
postgres (PostgreSQL) 18.4
继续检查:
cmd
"D:\Program Files\PostgreSQL\18\bin\postgres.exe" --version
结果:
text
postgres (PostgreSQL) 18.4
没有问题。
检查 initdb 版本
cmd
initdb --version
输出:
text
initdb (PostgreSQL) 18.4
继续检查:
cmd
"D:\Program Files\PostgreSQL\18\bin\initdb.exe" --version
输出:
text
initdb (PostgreSQL) 18.4
版本一致。
检查 EXE 文件版本
继续查看 Windows 文件版本:
cmd
wmic datafile where name="D:\\Program Files\\PostgreSQL\\18\\bin\\postgres.exe" get Version
输出:
text
18.0.4.0
继续:
cmd
wmic datafile where name="D:\\Program Files\\PostgreSQL\\18\\bin\\initdb.exe" get Version
输出:
text
18.0.4.0
说明:
两个 EXE 完全一致。
第二轮排查
很多博客都会建议:
查看 PATH。
执行:
cmd
where postgres
结果:
text
INFO: Could not find files for the given pattern(s).
说明:
PATH 中并没有其它 postgres.exe。
继续:
cmd
dir "D:\Program Files\PostgreSQL\18\bin\postgres.exe"
能够找到:
text
postgres.exe
所以:
不是 PATH 找错程序。
第三轮排查
怀疑 DLL 冲突。
查看:
cmd
where libpq.dll
结果:
text
D:\Program Files\PostgreSQL\18\bin\libpq.dll
继续:
cmd
where libintl*.dll
结果:
text
D:\Program Files\PostgreSQL\18\bin\libintl-9.dll
继续:
cmd
where icu*.dll
输出:
text
D:\Program Files\PostgreSQL\18\bin\icudt77.dll
D:\Program Files\PostgreSQL\18\bin\icuin77.dll
D:\Program Files\PostgreSQL\18\bin\icuuc77.dll
没有发现其它 PostgreSQL DLL。
继续使用 ProcMon 排查。
发现 postgres.exe 加载的 DLL 全部来自:
D:\Program Files\PostgreSQL\18\bin
没有:
- Miniconda DLL
- 旧 PostgreSQL DLL
- MySQL DLL
- 其它目录 DLL
说明:
DLL 冲突基本可以排除。
第四轮排查
怀疑安装包损坏。
重新计算 SHA256:
powershell
Get-FileHash "postgres.exe"
Get-FileHash "initdb.exe"
Get-FileHash "postgresql-18.4-2-windows-x64.exe"
全部正常。
重新下载安装包。
问题依旧。
第五轮排查
怀疑安装目录。
尝试:
D:\pgsql18
重新安装。
失败。
尝试:
C:\PostgreSQL
失败。
说明:
安装路径无关。
第六轮排查
怀疑 Locale。
尝试:
Default
失败。
继续:
English_United States
失败。
继续:
修改系统区域
修改语言
修改 UTF-8
全部失败。
第七轮排查
怀疑 Administrator 权限。
使用:
右键
以管理员身份运行
安装。
失败。
继续:
新建目录。
失败。
关闭 Clash Verge。
失败。
关闭 Defender。
失败。
全部失败。
真正的问题出现了
后来突然发现:
每次打开 CMD,
第一行都会自动出现:
text
Active code page: 65001
例如:
text
Microsoft Windows ...
Active code page:65001
C:\>
正常 Windows CMD 并不会这样。
说明:
CMD 每次启动都会自动执行:
cmd
chcp 65001
继续检查:
cmd
reg query "HKCU\Software\Microsoft\Command Processor" /v AutoRun
以及:
cmd
reg query "HKLM\Software\Microsoft\Command Processor" /v AutoRun
如果看到:
text
AutoRun
chcp 65001
或者:
text
AutoRun
@chcp 65001
那么就说明:
CMD 启动时自动执行了 UTF-8 切换。
为什么这会导致 PostgreSQL 初始化失败?
PostgreSQL 安装程序内部实际上会调用:
initdb
而 initdb 又会调用:
postgres -V
正常情况下:
应该输出:
text
postgres (PostgreSQL) 18.4
但是由于控制台自动输出:
text
Active code page:65001
最终得到的是:
text
Active code page:65001
postgres (PostgreSQL) 18.4
initdb 在比较版本字符串时,发现输出内容与预期不一致,于是误判为:
text
program "postgres"
was not the same version
as initdb
实际上:
postgres.exe 与 initdb.exe 完全是同一个版本。
如何验证是不是控制台环境导致的问题?
不要使用 CMD。
直接打开 PowerShell。
执行:
powershell
initdb -D tempdata
我的输出如下:
text
属于此数据库系统的文件宿主为用户 "Administrator".
数据库集簇将以区域环境设置进行初始化.
正在创建目录 ...
成功
正在创建配置文件 ...
成功
正在运行启动脚本 ...
成功
正在同步数据到磁盘 ...
成功
成功。
你现在可以用下面的命令开启数据库服务器
这说明:
PostgreSQL 本身没有任何问题。
真正失败的是安装阶段的初始化过程,而不是数据库程序本身。
解决方案
如果安装程序初始化失败,可以直接手动初始化数据库。
进入 PostgreSQL 的 bin 目录:
powershell
cd "D:\Program Files\PostgreSQL\18\bin"
执行:
powershell
.\initdb.exe -D "D:\Program Files\PostgreSQL\18\data"
如果看到:
text
Success.
说明数据库已经初始化完成。
后续只需要启动 PostgreSQL 即可,不需要重新安装。
安装完成后如何启动 PostgreSQL
如果你已经按照上面的方法成功执行了:
powershell
initdb -D "D:\Program Files\PostgreSQL\18\data"
并看到:
text
Success.
You can now start the database server...
说明数据库已经初始化完成。
接下来只需要启动 PostgreSQL 即可。
方法一:使用 pg_ctl 启动(推荐)
进入 PostgreSQL 的 bin 目录:
powershell
cd "D:\Program Files\PostgreSQL\18\bin"
启动数据库:
powershell
.\pg_ctl.exe -D "D:\Program Files\PostgreSQL\18\data" -l logfile start
如果启动成功,会看到:
text
waiting for server to start....
server started
参数说明:
| 参数 | 说明 |
|---|---|
-D |
指定数据目录 |
-l logfile |
将启动日志保存到 logfile 文件 |
建议所有日常使用都采用 pg_ctl。
方法二:直接运行 postgres(调试使用)
powershell
.\postgres.exe -D "D:\Program Files\PostgreSQL\18\data"
这种方式会一直占用当前终端。
关闭窗口以后:
PostgreSQL 立即退出
因此:
仅建议调试使用。
验证 PostgreSQL 是否已经启动
官方提供了一个检测工具:
powershell
.\pg_isready.exe
正常输出:
text
localhost:5432 - accepting connections
说明数据库已经开始监听。
如果输出:
text
no response
说明 PostgreSQL 尚未启动。
下面是这次 initdb 初始化出来的 PostgreSQL 默认结构说明(了解一下,建议重装程序,可跳到下一部分继续阅读):
md
## PostgreSQL 初始化信息(initdb 生成)
### 一、默认数据库集簇(Database Cluster)
你执行的命令:
```bash
initdb -D tempdata
创建了一个新的 数据库集簇(cluster):
- 数据目录:
tempdata - 编码:
UTF8 - 排序规则:
English_United States - 本地化编码映射:
GBK -> UTF8(自动替换) - 默认时区:
Asia/Shanghai
二、默认超级用户(Role / User)
初始化时自动创建的用户:
text
用户名(role):Administrator
说明:
- 这是当前 Windows 登录用户
- 被自动赋予 PostgreSQL 超级用户权限(superuser)
- 等价于 Linux 上的
postgres用户
三、默认数据库(Database)
初始化会自动创建 3 个数据库:
1. postgres(默认管理数据库)
text
数据库名:postgres
用途:系统默认连接库 / 管理用途
2. template0(模板库,不可修改)
text
数据库名:template0
用途:原始模板数据库(只读备份模板)
3. template1(默认模板库)
text
数据库名:template1
用途:创建新数据库时的默认模板
四、默认角色结构
初始化后默认只有一个关键角色:
text
Administrator(超级用户)
属性:
- SUPERUSER
- CREATEDB
- CREATEROLE
- LOGIN
五、默认连接方式
初始化后本地连接默认配置:
text
host : local (localhost)
auth : trust(本地连接无密码)
port : 5432
六、启动数据库方式
bash
pg_ctl -D tempdata -l logfile start
七、连接数据库方式
bash
psql -U Administrator -d postgres
八、重要提示(安全)
当前配置:
text
本地连接 = trust(无需密码)
建议生产环境修改为:
- scram-sha-256
- 或 md5 + password
通过修改:
text
tempdata/pg_hba.conf
九、总结
你当前初始化后的结构:
- 用户:admin(Administrator)
- 数据库:postgres / template0 / template1
- 权限:超级用户
- 编码:UTF8
- 端口:5432
建议直接重装软件,使用安装软件时默认的用户(postgres)、数据库(postgres)
使用 psql 连接 PostgreSQL
进入 bin 目录:
powershell
cd "D:\Program Files\PostgreSQL\18\bin"
连接默认数据库:
powershell
.\psql.exe -U postgres
或者:
powershell
.\psql.exe -U postgres -d postgres
如果提示:
text
Password for user postgres:
输入安装(或 initdb)时设置的密码即可。
连接成功以后:
text
psql (18.4)
postgres=#
出现:
text
postgres=#
说明已经成功进入 PostgreSQL。
第一次连接建议执行的 SQL
查看 PostgreSQL 版本:
sql
SELECT version();
例如:
text
PostgreSQL 18.4
查看当前数据库:
sql
SELECT current_database();
输出:
text
postgres
查看当前用户:
sql
SELECT current_user;
输出:
text
postgres
查看服务器时间:
sql
SELECT now();
查看所有数据库:
sql
\l
例如:
text
postgres
template0
template1
查看所有角色:
sql
\du
查看当前连接信息:
sql
\conninfo
例如:
text
You are connected to database "postgres"
退出 psql:
sql
\q
创建一个测试数据库
创建数据库:
sql
CREATE DATABASE testdb;
切换数据库:
sql
\c testdb
创建测试表:
sql
CREATE TABLE userinfo(
id SERIAL PRIMARY KEY,
name VARCHAR(50),
age INT
);
插入测试数据:
sql
INSERT INTO userinfo(name,age)
VALUES
('Tom',20),
('Alice',25),
('Bob',30);
查询:
sql
SELECT * FROM userinfo;
输出:
text
id | name | age
----+-------+-----
1 | Tom | 20
2 | Alice | 25
3 | Bob | 30
说明数据库已经可以正常读写。
停止 PostgreSQL
仍然进入:
powershell
D:\Program Files\PostgreSQL\18\bin
执行:
powershell
.\pg_ctl.exe -D "D:\Program Files\PostgreSQL\18\data" stop
正常输出:
text
waiting for server to shut down....
server stopped
重启 PostgreSQL
powershell
.\pg_ctl.exe -D "D:\Program Files\PostgreSQL\18\data" restart
查看 PostgreSQL 状态
powershell
.\pg_ctl.exe -D "D:\Program Files\PostgreSQL\18\data" status
例如:
text
server is running
注册 Windows 服务(推荐,此步骤手动模式下才需执行;重装成功可自动添加到服务,不必执行此步骤)
如果希望 PostgreSQL 开机自动启动,
建议注册 Windows 服务。
管理员 PowerShell:
powershell
.\pg_ctl.exe register `
-N PostgreSQL18 `
-D "D:\Program Files\PostgreSQL\18\data"
启动服务:
powershell
net start PostgreSQL18
停止服务:
powershell
net stop PostgreSQL18
查看服务:
powershell
sc query PostgreSQL18
卸载服务:
powershell
.\pg_ctl.exe unregister -N PostgreSQL18
这样 PostgreSQL 就变成了标准 Windows 服务。
pgAdmin 如何连接
如果安装了 pgAdmin 4,
新增服务器:
Host
localhost
端口:
5432
用户名:
postgres
密码:
安装时设置的密码
保存即可连接。
常见问题
1、提示:
text
could not connect to server
说明数据库没有启动。
执行:
powershell
.\pg_ctl.exe -D "D:\Program Files\PostgreSQL\18\data" start
即可。
2、提示:
text
FATAL:
password authentication failed for user "postgres"
说明密码错误。
重新输入初始化时设置的密码即可。
3、提示:
text
Is another postmaster already running on port 5432?
说明端口被占用。
查看:
powershell
netstat -ano | findstr 5432
如果已有 PostgreSQL:
停止旧实例。
或者修改:
postgresql.conf
中的:
text
port = 5433
然后重新启动。
4、提示:
text
The database system is starting up
说明 PostgreSQL 正在启动。
等待几秒即可。
5、提示:
text
Data directory has wrong ownership
说明数据目录权限有问题。
Windows 下一般重新执行:
powershell
initdb
即可。
6、提示:
text
server closed the connection unexpectedly
建议查看:
logfile
或者:
pg_log
查看详细日志。
本次排查总结
本次问题经历了几乎完整的一次 Windows PostgreSQL 安装排查。
依次验证了:
✅ PostgreSQL 版本
✅ initdb 版本
✅ PATH
✅ DLL 冲突
✅ ProcMon
✅ SHA256
✅ 文件版本
✅ Locale
✅ 管理员权限
✅ 安装目录
最终定位到:
安装程序在初始化数据库阶段失败,
而 PostgreSQL 本身没有任何问题。
手动执行:
powershell
initdb -D "数据目录"
即可正常初始化数据库。
如果你的日志中同时出现:
text
Problem running post-install step.
The database cluster initialisation failed.
以及:
text
program "postgres" was found
but was not the same version as initdb
建议按照本文的顺序排查:
- 检查
postgres --version - 检查
initdb --version - 检查
where postgres - 检查 DLL 是否冲突
- 检查 CMD 是否自动输出
Active code page: 65001 - 尝试使用 PowerShell 手动执行
initdb - 使用
pg_ctl启动 PostgreSQL - 使用
psql验证连接
按照上述步骤,基本可以解决绝大多数 Windows 平台 PostgreSQL 初始化失败的问题。