我有以下代码:自动重新连接postgresq数据库
import psycopg2
conn = psycopg2.connect(database="****", user="postgres", password="*****", host="localhost", port="5432")
print ("Opened database successfully")
cur = conn.cursor()
cur.execute('''select * from xyz''')
print ("Table created successfully")
conn.commit()
conn.close()
喜欢我有一些50 -60复杂的查询,但问题是有时在postgre SQL数据库抛出下面的错误
Traceback (most recent call last):
File "C:/Users/ruw2/Desktop/SQL.py", line 87, in <module>
cur.execute('''select * from xyz;''')
psycopg2.DatabaseError: server conn crashed?
server closed the connection unexpectedly.
This probably means the server terminated abnormally before or while processing the request.
看起来连接丢失了,我怎么能自动连接Postgre数据库 感谢您的帮助
很难猜测,最有可能的连接超时,在这种情况下,大多数数据库适配器提供您一个标志,自动重新连接。另一个选择是服务器真的崩溃,在这种情况下检查日志;还有一点是,互联网在你的客户端和数据库服务器之间是片状的(如果localhost:5432被转发到某处)。另一种可能性是死锁(不太可能),或者结果集太大以至于服务器或客户端死亡(尝试更新的版本)。 –
感谢您的回复,但是当我手动运行它时,它运行良好,但是当我通过Python代码运行它时,它会自动完成,最终会出现错误。是否有任何自动重新连接选项... – iahmed
你让我在“Python代码自动化”中失去了我。看起来你有更大的问题... –