首页 > 解决方案 > 无法从 python 脚本和命令行连接到数据库,但可以连接到 PGadmin - 致命:用户“postgres”的身份验证失败

问题描述

我知道类似的问题已经被问了大约 2000 次,但到目前为止我找到的答案都没有帮助我。

因此,我可以通过 SSH 隧道通过 PGAdmin 连接到我的数据库,我也可以使用su - postgres psql,但是当我这样做时su - postgrespsql -U postgres -h localhost就会出现错误FATAL: Ident authentication failed for user "postgres"

当我尝试使用我的 python 脚本连接到数据库时,也会发生同样的事情。

当然,我尝试pg_hba.conf以多种方式编辑我的文件,将所有内容设置为trust,passwordmd5. 每次都说同样的话。

目前我的pg_hba.conf样子是这样的:

local all all md5

host all all 127.0.0.1/32 md5

host all all ::1/128 md5

是的,我确实在更改之间重新启动了服务器。我不知道这里可能是什么问题,任何帮助将不胜感激!

标签: postgresqlcentos7

解决方案


好的,我终于设法解决了这个问题!

此处的错误消息完全具有误导性。问题是“localhost”没有解析为 127.0.0.1,因为 /etc/hosts 文件有两行指定 localhost:

127.0.0.1 localhost.localdomain localhost localhost4.localdomain4 localhost4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

使用 127.0.0.1 或离开只::1 localhost6 localhost6.localdomain6解决了问题。

希望这对将来的某人有所帮助:)


推荐阅读