Psycopg2 reporting pg_hba.conf error

2019-04-06 19:02发布

I've run into a weird situation while trying to use PostgreSQL and Psycopg2. For some reason, every time I attempt to connect to the postgre database via python, I get the following error:

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

Naturally, I checked pg_hba.conf to see what the issue was, but everything appeared to be configured correctly as far as I can see:

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

In addition, I've found that I can connect to the database via psql as I would expect:

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

Anyone have any ideas as to what could be going on here? Thanks in advance!

2条回答
放我归山
2楼-- · 2019-04-06 19:03

Typical explanations include:

  • You are connecting to the wrong server.
    Is the DB server running on the same host as Python does?

  • You got the wrong port.
    Check the server log if you see a connection attempt. You have to log connections for that, of course. See the config parameter log_connections.

  • You did not reload (SIGHUP) the server after changing pg_hba.conf - or reloaded the wrong cluster (if you have multiple DB clusters).
    Use pg_ctl or pg_ctlcluser on Debian and derivatives for that.

查看更多
祖国的老花朵
3楼-- · 2019-04-06 19:18

I recently got into this same issue and I found the solution for this problem.

System:

  1. I have an application server (with these packages installed python, django, psycopg2 and postgres client 9.6.1 (postgresql-9.6.1.tar.gz)), for instance ip address 10.0.0.1(a private address).
  2. And AWS postgres RDS server "aws_rds_host_name" or any database IP address.

Error:

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

Solution:

While installing the postgres client 9.6.1 source package in application server 10.0.0.1, we have to pass an argument "--with-openssl". I suggest to remove the existing the postgres client and install with below steps.

  1. Download the postgres client source package 9.6.1 (postgresql-9.6.1.tar.gz)
  2. Untar the package postgresql-9.6.1.tar.gz.
  3. ./configure --prefix="your_preferred_postgres_path_if_needed" --with-openssl (this '--with-openssl' argument is important to get rid of that error)
  4. make
  5. make install
  6. After successful installation, that error didn't occur when we ran the django project with psycopg2.

I hope this solution helps someone.

查看更多
登录 后发表回答