Remote connection issues with psycopg2 and postgresql

2.7k views Asked by At

I'm trying to connect to a postgresql database using the following python code:

try:
    conn = psycopg2.connect("host = '10.47.65.237' dbname = 'testDB' user = 'pi' password = 'raspberry'")
except:
    print("Unable to connect to testDB at 10.47.65.237. Sending Alert.")

This code works with localhost 127.0.0.1 but when I go to a different machine and try to run it with its ip above it won't connect.

Things I've done: 1. Port 5432 is open 2. edited postgresql.conf by adding the line "listen_addresses='10.47.65.138'" 3. edited pg_hba.conf by adding the following configuration "host all all 10.47.65.138 md5"

Any other things I could try or I'm missing?

1

There are 1 answers

0
200_success On BEST ANSWER

Running telnet 10.47.65.237 5432 on the client should result in a Connection Refused error, which indicates that the problem has nothing to do with psycopg2.

You have misconfigured the server. listen_addresses controls which IPs the server will answer on, not which IPs the server will permit connections from. Your server's postgresql.conf should have either listen_addresses='10.47.65.237' or listen_addresses='*'. Edit the configuration and restart PostgreSQL on the server, then you should be able to connect successfully using telnet and psycopg2.