# psycopg2 OperationalError trouble | Python/Flask/Psycopg2

**URL:** <https://community.fly.io/t/psycopg2-operationalerror-trouble-python-flask-psycopg2/7522>\
**Category:** Questions / Help\
**Tags:** postgres\
**Created:** [September 29, 2022, 1:12am UTC](https://community.fly.io/t/psycopg2-operationalerror-trouble-python-flask-psycopg2/7522 "2022-09-29T01:12:37Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![BenFifthHour](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/benfifthhour/32/9854_2.png) [@BenFifthHour](https://community.fly.io/u/BenFifthHour)\
**Post date:** [November 1, 2022, 3:20pm UTC](https://community.fly.io/t/psycopg2-operationalerror-trouble-python-flask-psycopg2/7522/3 "2022-11-01T15:20:50Z")

</div>

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.

> [@PostgreSQL "Connection is closed" error after a few minutes of activity](https://community.fly.io/t/postgresql-connection-is-closed-error-after-a-few-minutes-of-activity/4768):
>
> Hello there! I’ve successfully deployed my Python app with a PostgreSQL cluster. The app manages the DB connection using [SQLAlchemy](https://www.sqlalchemy.org/) with the [asyncpg](https://github.com/MagicStack/asyncpg) driver. However, I regularly get a server error because the connection between the app and PostgreSQL has been unexpectedly closed. It usually happens after a few minutes (~ 15-30 minutes) of inactivity. Here is the error in details: InterfaceError: connection is closed File "sqlalchemy/dialects/postgresql/asyncpg.py", line 709, in \_start\_tran…

---

_[View the full topic](https://community.fly.io/t/psycopg2-operationalerror-trouble-python-flask-psycopg2/7522)._
