2

I'have a little problem when I want to connect to my DB with psycopg2 and python. I have this little script :

#!/usr/bin/python3



import time 
import psycopg2
import sys

def main():

    # Get a connection 
    conn = psycopg2.connect(database='my_db', host='10.10.2.1', port='5433', user='me', password='my_password_that_i_dont_show_here')

    # conn.cursor will return a cursor object, to perform queries
    cursor = conn.cursor()

    # Execute our query
    cursor.execute("select date(created_at), email, firstname, lastname, locale from users where date(created_at) = current_date;")

    # retrieve the records from the database
    records = cursor.fetchall()

    print(records)

if __name__ == "__main__":
    main()

That worked well on windows but now I'm on ubuntu and I have this error :

Traceback (most recent call last):
  File "Bureau/script.py", line 29, in <module>
    main()
  File "Bureau/script.py", line 15, in main
    conn = psycopg2.connect(database='my_db', host='10.10.2.1', port='5433', user='me', password='best_password_ever')
  File "/usr/local/lib/python3.5/dist-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection timed out
    Is the server running on host "10.10.2.1" and accepting
    TCP/IP connections on port 5433?
Grechka Vassili
  • 853
  • 4
  • 15
  • 32

0 Answers0