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

Windows10下Python testing.postgresql报错:initdb找不到同目录postgres

问题描述

部分使用testing.postgresql的Python单元测试在Windows 10设备上无法正常运行,报错提示initdb需要postgres程序但无法在同目录找到。相关已知信息如下:

  • 已确认initdb、postgres程序均存放在PostgreSQL安装路径的bin目录下,直接在该目录运行initdb也报相同错误
  • 安装路径的bin、lib目录均已加入系统PATH,运行which initdb和which postgres均可返回正确路径
  • 已尝试同类问题相关的公开解决方案均无效,该测试在macOS和GitHub工作流中可正常运行
  • 设备安装的是PostgreSQL 14,使用Cygwin终端、Windows cmd运行均会出现相同错误
错误日志

在PostgreSQL安装的bin目录下运行initdb的输出如下:

$ ./initdb -D example
The system cannot find the path specified.
child process exited with exit code 1
initdb: error: The program "postgres" is needed by initdb but was not found in the
same directory as "C:/Program Files/PostgreSQL/14/bin/initdb.exe".
Check your installation.

同目录下执行ls命令可确认postgres.exe确实存在:

$ ls
clusterdb.exe   icutu67.dll            libiconv-2.dll       libxslt.dll            pg_dump.exe               pg_restore.exe       psql.exe                           wxmsw313u_core_vc_x64_custom.dll
createdb.exe    icuuc67.dll            libintl-9.dll        oid2name.exe           pg_dumpall.exe            pg_rewind.exe        reindexdb.exe                      wxmsw313ud_aui_vc_x64_custom.dll
createuser.exe  initdb.exe             liblz4.dll           pg_amcheck.exe         pg_isolation_regress.exe  pg_test_fsync.exe    stackbuilder.exe                   wxmsw313ud_html_vc_x64_custom.dll
dropdb.exe      isolationtester.exe    libpgtypes.dll       pg_archivecleanup.exe  pg_isready.exe            pg_test_timing.exe   vacuumdb.exe                       wxmsw313ud_xrc_vc_x64_custom.dll
dropuser.exe    libcrypto-1_1-x64.dll  libpq.dll            pg_basebackup.exe      pg_receivewal.exe         pg_upgrade.exe       vacuumlo.exe                       zic.exe
ecpg.exe        libcurl.dll            libpq_pipeline.exe   pg_checksums.exe       pg_recvlogical.exe        pg_verifybackup.exe  wxbase313u_vc_x64_custom.dll       zlib1.dll
icudt67.dll     libcurl.lib            libssl-1_1-x64.dll   pg_config.exe          pg_regress.exe            pg_waldump.exe       wxbase313u_xml_vc_x64_custom.dll
icuin67.dll     libecpg.dll            libwinpthread-1.dll  pg_controldata.exe     pg_regress_ecpg.exe       pgbench.exe          wxbase313ud_net_vc_x64_custom.dll
icuio67.dll     libecpg_compat.dll     libxml2.dll          pg_ctl.exe             pg_resetwal.exe           postgres.exe         wxmsw313u_adv_vc_x64_custom.dll
解决方案

1. 修复系统权限问题

PostgreSQL默认安装在C:\Program Files\系统目录下,普通用户没有该目录的写入权限,initdb执行时需要创建临时调用文件,权限不足会导致无法识别同目录下的postgres程序。可二选一处理:

  • 将PostgreSQL的bin目录整体复制到非系统权限路径,比如C:\PostgreSQL\14\bin,再将新路径加入系统PATH,重启终端后重试
  • 右键点击终端(cmd/Cygwin),选择「以管理员身份运行」后再执行initdb或单元测试

2. 解决路径兼容问题

你使用的是Windows原生版本的PostgreSQL,Cygwin/Git Bash这类模拟POSIX环境的终端会自动转换路径格式,导致initdb识别路径时出现冲突:

  • 优先使用Windows cmd/PowerShell执行测试,不要使用POSIX模拟终端
  • 如果必须使用Cygwin,直接调用Windows格式的绝对路径执行initdb:C:\Program Files\PostgreSQL\14\bin\initdb.exe -D example,不要使用./initdb相对路径调用

3. 调整testing.postgresql配置

Python的testing.postgresql库默认从PATH搜索程序,你可以手动指定PostgreSQL的bin路径,避免搜索异常,在测试代码开头添加如下配置即可:

import testing.postgresql
testing.postgresql.POSTGRESQL_BIN_PATH = r'C:\Program Files\PostgreSQL\14\bin'

如果仍然报错,可以改用Windows兼容性更好的testcontainers-postgresql库替代testing.postgresql。

4. 排查安全软件拦截

部分杀毒软件、终端安全管理工具会拦截postgres.exe的临时调用,导致initdb判定程序不存在。可以临时关闭安全软件的实时防护,再执行测试验证是否是拦截导致的问题。

内容的提问来源于stack exchange,提问作者6675636b796f75

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 18:45:00