Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Psycopg2 reporting pg_hba.conf error

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!

like image 247
Steve Gattuso Avatar asked May 21 '12 23:05

Steve Gattuso


People also ask

How configure Pg_hba conf?

Start PgAdminIII, connect to the PostgreSQL instance as the postgres super user, connect to the database, click Tools, point to Server Configuration, then click pg_hba. conf. Start Notepad or another text editor application and open the pg_hba. conf file from the PostgreSQL installation directory.

What is Pg_hba Conf entry?

conf File. Client authentication is controlled by a configuration file, which traditionally is named pg_hba. conf and is stored in the database cluster's data directory. ( HBA stands for host-based authentication.)

Where is the Pg_hba conf?

By default, the file is named pg_hba. conf. By default, on RHEL 7 , the file is at /var/lib/pgsql/data/, and on Windows, the file is at C:\Program Files\PostgreSQL\ version_number \data\.


1 Answers

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.

like image 54
Erwin Brandstetter Avatar answered Sep 30 '22 16:09

Erwin Brandstetter