1

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?

3
  • 1
    If you run telnet 10.47.65.237 5432, do you get a successful connection, or do you get a Connection Refused error, or does it just hang? Commented Jun 8, 2015 at 16:36
  • To debug this problem, you may try to use a 3rd party app to see if you can access your remote db. If you're unable to connect, it must be server side issue. Once you confirm it, you can debug client side by checking internet connection and etc. Commented Jun 8, 2015 at 16:38
  • It immediately gave me a connection refused error when I tried to telnet to it on 5432. Commented Jun 8, 2015 at 17:59

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.