我有一个带有10+解析器的项目,最后有以下代码:
`
cursor = conn.cursor()
my_file = open(r'csv\file.csv')
sql_statement = """
CREATE TEMP TABLE temp
(
LIKE vhcl
)
ON COMMIT DROP;
COPY temp FROM STDIN WITH
CSV
HEADER
DELIMITER AS ',';
INSERT INTO vhcl
SELECT *
FROM temp
ON CONFLICT (id) DO UPDATE SET name= EXCLUDED.name"""
cursor.copy_expert(sql=sql_statement, file=my_file)
conn.commit()
cursor.close()“直到几周前我才开始发现这些错误,一切都很好:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.我注意到,如果解析器工作时间小于10分钟,我就不会得到这些错误。
我尝试创建一个单独的函数,在解析器结束工作后将数据添加到DB。它仍然给了我那个错误。奇怪的是,我在我的家用pc上运行了我的解析器,而且它运行得很好,如果我用相同的函数手动添加数据,但是在不同的文件中,它也可以正常工作。
我问过禁止使用db的IP,但没关系。所以我不知道为什么会有这个错误。
PostgreSQL日志

发布于 2022-11-30 08:16:45
最后,我找到了一个解决办法。我还是不知道问题出在哪里。这不是连接问题,因为一些具有相同IP和相同网络连接的解析器正常工作。我仍然能够用相同的脚本添加数据,但是在一个单独的项目文件中。
我的解决方案是在连接中添加“持生”设置:
conn = psycopg2.connect(
host=hostname,
dbname=database,
user=username,
password=password,
port=port_id,
keepalives=1,
keepalives_idle=30,
keepalives_interval=10,
keepalives_count=5)发布于 2022-11-29 14:09:09
你有网络问题。
服务器和客户端都抱怨对方意外地挂断了它们。所以是中间的一些错误的网络组件切断了这条线。你有两个选择:
有关更多细节,您可能希望阅读这。
https://stackoverflow.com/questions/74611976
复制相似问题