user3658457
user3658457

Reputation: 315

Trouble connecting to Postgresql DB through Python's psycopg2

I have been using a postgres database with pgAdmin3, and have never had any issues with connecting to the database. Today I tried accessing the database through Python using the following code:

import psycopg2
conn_string = "dbname='db0r0373s42fkg' user='ucaj9jki7hfthu' host='ec2-107-20-189-29.compute-1.amazonaws.com' password='secret'"
conn = psycopg2.connect(conn_string)
conn.close()

and I get the following error:

FATAL:  no pg_hba.conf entry for host "207.204.247.209", user "ucaj9jki7hfthu", database "db0r0373s42fkg", SSL off

From looking around this site, it seems that error occurs when the database is not set up to allow me to sign in with these credentials, but why does it work through pgadmin but not Python?

I saw another post with a similar question(Psycopg2 reporting pg_hba.conf error), but I didn't quite understand their answer and I'm not sure if I'm having the same problem Thanks so much for your help.

Upvotes: 1

Views: 2420

Answers (2)

Erin Call
Erin Call

Reputation: 1784

It looks like you have the same problem as this question: You're not connecting over SSL. Try passing sslmode='require' to psycopg2.connect.

Upvotes: 1

FirebladeDan
FirebladeDan

Reputation: 1069

Did you check what the poster said to check?
On your server run the following command:
cd /etc/postgresql/9.1/main/postgresql.conf

It might not be the same version so watch 9.1 but pretty much navigate to postgresql.conf. In this file will be the listed port. If running from the same machine make sure they aren't consuming the same port. pgadmin is using 5432 so make sure this file says it is as well.

Upvotes: 0

Related Questions