Python Psycopg2报告pg_hba.conf错误

Python Psycopg2报告pg_hba.conf错误,python,postgresql,psycopg2,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", u

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

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

典型的解释包括:

  • 您连接到了错误的服务器
    DB服务器是否与Python在同一主机上运行

  • 您的端口错误
    如果看到连接尝试,请检查服务器日志。当然,您必须为此记录连接。看

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


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

系统:

  • 我有一个应用服务器(安装了
    python
    django
    psycopg2
    postgres客户端9.6.1
    postgresql-9.6.1.tar.gz
    ),例如
    ip地址10.0.0.1
    (一个私人地址)
  • 和AWS postgres RDS服务器“
    AWS\u RDS\u主机名
    ”或任何数据库IP地址
  • 错误:

    # 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
    
    django.db.utils.OperationalError:致命:主机没有pg_hba.conf条目
    “10.0.0.1”、用户“您的用户”、数据库“您的数据库”、SSL关闭

    解决方案:

    # 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
    
    在application server
    10.0.0.1
    中安装
    postgres client 9.6.1
    源程序包时,我们必须传递一个参数“
    ——使用openssl
    ”。我建议删除现有的
    postgres客户端
    ,并按以下步骤安装

  • 下载
    postgres客户端源代码包9.6.1
    postgresql-9.6.1.tar.gz
  • 卸载包
    postgresql-9.6.1.tar.gz
  • /configure--prefix=“如果需要,您的\u首选\u postgres\u路径”--openssl(此'--with openssl'
    参数对于消除该错误很重要)
  • 制造
  • 安装
  • 成功安装后,当我们使用
    psycopg2
    运行django项目时,没有发生该错误

  • 我希望这个解决方案能帮助一些人。

    让我很尴尬的是,你完全正确!对于任何可能处于类似情况的人:问题源于从8.4->9.1升级。事实证明,新服务器将自己配置为在5433上运行,等待来自旧数据库的数据传输。检查端口的
    /etc/postgre/9.1/main/postgre.conf
    。@SteveGattuso:我们所有人都会这样。不必感到尴尬。:)顺便说一句,配置文件的完整路径通常是
    /etc/postgresql/9.1/main/postgresql.conf
    。感谢您的检查表。事实上,我不得不重新装弹。我在运行postgresql 11的debian 10上使用了
    sudo systemctl reload postgresql