server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.

1.链接出错是因为链接时间太长,pg自己断开了链接,所以可以在需要执行sql语句的位置进行链接,然后close就行

一定要记得close!

try:
conn = pg_conn()
cur = conn.cursor()
cur.execute(sql_1)
conn.commit()
conn.close()
except Exception as e:
print(e)
print("数据无法同步至数据库")

 

 

2.

 

 

地址:https://www.postgresql.org/docs/9.3/libpq-connect.html
(文章底部)
# 获取pg数据库对象
def pg_conn():
    conn = psycopg2.connect(
        database='xxxx',
        user='xxxx',
        password='xxxx',
        host='xxxx',
        port='xxxx',
        keepalives=1,
        keepalives_idle=30,
        keepalives_interval=10,
        keepalives_count=5
   )
   return conn

 这个方法别人说可以,但是我实际操作的时候并不可行,或许因为我在链接以后耽误的时间太长?

posted @ 2022-08-24 17:12  黑山老道  阅读(5449)  评论(0编辑  收藏  举报