PostgreSQL's OperationalError: server closed the connection unexpectedly
Created:22-06-04 14:16
Updated:23-01-28 22:55
In Python, Psycopg2 module is usually used to connect to PostgreSQL. When I used it, I got the below exception when access a connection has been idle for a while:
After searching for a while, I found there seems to be 2 reasons that could possibly cause it. And the 2nd reason is seldom mentioned:
Please see below codes:
I didn’t include the last 4 parameters at the beginning (so got the exception), and after I added them the problem went away. Those parameters actually define how a connection should be kept alive.
I didn’t see the psycopg2 documents introduced these keep-alive parameters, but psycopg2 doc does mention that: “Any other connection parameter supported by the client library/server can be passed either in the connection string or as a keyword. The PostgreSQL documentation contains the complete list of the "https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS" supported parameters. Also note that the same parameters can be passed to the client library using "https://www.postgresql.org/docs/current/static/libpq-envars.html" environment variables. ” So those keywords are actually defined and accepted by the underlying PostgreSQL client library. You can find the keep-alive and also more parameters in the document of the PostgreSQL library |
|