Python Psycopg2无法连接到服务器
当我想用psycopg2和python连接到数据库时,我遇到了一个小问题。我有一个小剧本:Python Psycopg2无法连接到服务器,python,psycopg2,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_pas
#!/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?
回溯(最近一次呼叫最后一次):
文件“Bureau/script.py”,第29行,在
main()
文件“Bureau/script.py”,第15行,主
conn=psycopg2.connect(数据库='my\u db',主机='10.10.2.1',端口='5433',用户='me',密码='best\u password')
文件“/usr/local/lib/python3.5/dist packages/psycopg2/_init__.py”,第164行,在connect中
连接=连接(dsn,连接工厂=连接工厂,异步=异步)
psycopg2.OperationalError:无法连接到服务器:连接超时
服务器是否在主机“10.10.2.1”上运行并接受
端口5433上的TCP/IP连接?
可能与此有关:我发现了错误!在windows上我有一个VPN,但在ubuntu上没有:)