python - Psycopg2 报告 pg_hba.conf 错误

标签 python postgresql psycopg2

我在尝试使用 PostgreSQL 和 Psycopg2 时遇到了一个奇怪的情况。出于某种原因,每次我尝试通过 python 连接到 postgre 数据库时,我都会收到以下错误:

psycopg2.OperationalError: FATAL:  no pg_hba.conf entry for host "127.0.0.1", user "steve", database "steve", SSL on
FATAL:  no pg_hba.conf entry for host "127.0.0.1", user "steve", database "steve", SSL off

当然,我检查了 pg_hba.conf 以查看问题所在,但据我所知,一切似乎都配置正确:

pg_hba.conf:

# TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

此外,我发现我可以像预期的那样通过 psql 连接到数据库:

$ psql -U steve -h 127.0.0.1
...
steve=>

有人知道这里会发生什么吗?提前致谢!

最佳答案

典型的解释包括:

  1. 您正在连接到错误的服务器
    数据库服务器是否与 Python 在同一主机上运行?

  2. 你得到了错误的端口
    如果看到连接尝试,请检查服务器日志。当然,您必须为此记录连接。查看config parameter log_connections .

  3. 您在更改 pg_hba.conf 后没有重新加载 (SIGHUP) 服务器 - 或者重新加载了错误的集群(如果您有多个数据库集群)。

    使用pg_ctl或 Debian 及其衍生产品上的 pg_ctlcluser

    或者,在使用 systemd(包括 Debian 和 friend )的现代 Linux 安装上,通常:

     sudo systemctl reload postgresql
    

    或者,如果有多个安装,请检查:

     sudo systemctl status postgres*
    

    然后用类似的东西重新找到你想要的那个:

     sudo systemctl reload postgresql@14-main
    

关于python - Psycopg2 报告 pg_hba.conf 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10693978/

相关文章:

sql - 为什么 postgresql 考虑这个子查询列名不完全限定且不明确

python - psycopg2.errors.SyntaxError : syntax error at or near "<"

postgresql - SQLAlchemy、Psycopg2 和 Postgresql COPY

python - psycopg2 不返回结果

python - 在 Python SQLAlchemy 中创建基于字符串的序列

python - 保存时 Pandas 的身材正在被削减

javascript - Flask WTForm 作为对 AJAX 调用的响应

postgresql - java - PSQLException : ERROR: syntax error at or near "$1"

python - 如何获取硬链接(hard link)的数量

postgresql - Postgresql中如何正确使用GROUP BY函数