19

我在尝试使用 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=>

有人对这里可能发生的事情有任何想法吗?提前致谢!

4

2 回答 2

18

典型的解释包括:

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

  • 弄错了端口
    如果您看到连接尝试,请检查服务器日志。当然,您必须为此记录连接。请参阅配置参数log_connections

  • 更改后您没有重新加载(SIGHUP) 服务器pg_hba.conf- 或重新加载了错误的集群(如果您有多个数据库集群)。
    在 Debian 及其衍生产品上使用pg_ctlor pg_ctlcluser

于 2012-05-21T23:50:31.507 回答
3

我最近遇到了同样的问题,我找到了解决这个问题的方法。

系统:

  1. 例如,我有一个应用程序服务器(安装了这些软件包pythondjango和( ))(一个私有地址)。psycopg2postgres client 9.6.1postgresql-9.6.1.tar.gzip address 10.0.0.1
  2. 和 AWS postgres RDS 服务器“ aws_rds_host_name”或任何数据库 IP 地址。

错误:

django.db.utils.OperationalError: FATAL: no pg_hba.conf entry for host "10.0.0.1", user "your_user", database "your_db", SSL off

解决方案:

在应用服务器中安装postgres client 9.6.1源码包时10.0.0.1,我们必须传递一个参数“ --with-openssl”。我建议删除现有的postgres client并按照以下步骤安装。

  1. 下载postgres client source package 9.6.1( postgresql-9.6.1.tar.gz)
  2. 解压包postgresql-9.6.1.tar.gz
  3. ./configure --prefix="your_preferred_postgres_path_if_needed" --with-openssl (this '--with-openssl'论点对于摆脱该错误很重要)
  4. 制作
  5. 进行安装
  6. 安装成功后,当我们使用psycopg2.

我希望这个解决方案可以帮助某人。

于 2017-05-24T01:03:00.833 回答