Psycopg2无法连接到服务器。

10 浏览
0 Comments

Psycopg2无法连接到服务器。

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

#!/usr/bin/python3
import time 
import psycopg2
import sys
def main():
    # 获取连接
    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会返回一个游标对象,用于执行查询
    cursor = conn.cursor()
    # 执行查询
    cursor.execute("select date(created_at), email, firstname, lastname, locale from users where date(created_at) = current_date;")
    # 从数据库检索记录
    records = cursor.fetchall()
    print(records)
if __name__ == "__main__":
    main()

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

Traceback (most recent call last):
  File "Bureau/script.py", line 29, in 
    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