psycopg2 OperationalError trouble | Python/Flask/Psycopg2

Hi I’m getting OperationError from psycopg2. This is my connection to postgres

c = psycopg2.connect(
		os.environ.get("DATABASE_URL"),
		keepalives=1,
                keepalives_idle=130,
                keepalives_interval=10,
		keepalives_count=15
		)

I don’t always get the error. It’s completely random, never had the error on Heroku but don’t think it’s Flyio issue. Has anyone had this issue?
I can see I get the error right after trying to use the connect but that’s it.
This is the complete error

psycopg2.OperationalError: server closed the connection unexpectedly
this probably means the server terminated abnormally
before or while processing the request.

Not sure how to troubleshoot this, I think it’s a postgres + flyio related thing idk

1 Like

I’m getting this issue too. It only occurs once and then the subsequent db op will succeed. I feel like it’s when my part of the postgres instance (using the free allocation) maybe has gone to sleep after a period (> 1 hour?) of inactivity. A wild guess!

1 Like

hi

I found this thread which may help. my guess was kind of right, the postgres db is closing idle connections older that aroudn 30 minutes.