Windows Podman v5.3.1连接PostgreSQL时密码认证失败求助
PostgreSQL容器密码认证失败问题解决(Windows + Podman 5.3.1)
问题描述
在Windows环境使用Podman v5.3.1部署PostgreSQL 12-alpine容器,执行以下命令:
podman run --name internet-archive -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:12-alpine
通过podman ps确认容器正常运行后,创建数据库:
podman exec -it internet-archive createdb --username=root --owner=root crawler
尝试连接数据库时:
psql -h localhost -p 5432 -U root -d crawler
输入密码secret后出现错误:
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "root"
容器内pg_hba.conf配置显示IPv6本地连接使用trust认证:
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust # Allow replication connections from localhost, by a user with the # replication privilege. local replication all trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust
可能原因及解决步骤
1. 本地PostgreSQL服务占用5432端口
Windows中localhost优先解析为IPv6地址(::1),如果本地有PostgreSQL服务运行并占用IPv6的5432端口,连接请求会被导向本地服务而非容器。
验证方法:
打开命令提示符执行:
netstat -ano | findstr :5432
若输出中包含非Podman/WSL相关进程(如postgres.exe),说明端口被占用。
解决方式:
- 停止本地PostgreSQL服务:
sc stop postgresql-x64-12 # 替换为你的本地PostgreSQL服务名,可通过`sc query type= service | findstr PostgreSQL`查询 - 或修改容器端口映射,避开冲突:
podman stop internet-archive podman rm internet-archive podman run --name internet-archive -p 5433:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:12-alpine # 用新端口连接 psql -h localhost -p 5433 -U root -d crawler
2. 强制使用IPv4连接
直接指定IPv4地址127.0.0.1连接,匹配容器内pg_hba.conf的trust规则(无需输入密码):
psql -h 127.0.0.1 -p 5432 -U root -d crawler
3. 确保容器内PostgreSQL监听IPv6地址
默认情况下PostgreSQL可能仅监听IPv4,导致IPv6连接请求无法到达容器。
操作步骤:
- 修改容器内
postgresql.conf的监听配置:podman exec -it internet-archive sed -i "s/^listen_addresses = .*/listen_addresses = '*'/" /var/lib/postgresql/data/postgresql.conf - 重启容器使配置生效:
podman restart internet-archive - 再次尝试用
localhost连接:psql -h localhost -p 5432 -U root -d crawler
内容的提问来源于stack exchange,提问作者Levia
相关产品推荐
相关产品推荐

