Psycopg2无法连接到服务器

时间:2016-05-10 09:59:09

标签: python psycopg2

当我想用psycopg2和python连接到我的数据库时,我有一点问题。我有这个小脚本:

#!/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()

在Windows上运行良好,但现在我在ubuntu上运行了这个错误:

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?

0 个答案:

没有答案