# Persistent connection to Postgres database

**URL:** <https://community.fly.io/t/persistent-connection-to-postgres-database/5663>\
**Category:** Questions / Help\
**Tags:** postgres\
**Created:** [June 16, 2022, 3:54am UTC](https://community.fly.io/t/persistent-connection-to-postgres-database/5663 "2022-06-16T03:54:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![denishacks](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/denishacks/32/4056_2.png) [@denishacks](https://community.fly.io/u/denishacks)\
**Post date:** [June 16, 2022, 3:54am UTC](https://community.fly.io/t/persistent-connection-to-postgres-database/5663/1 "2022-06-16T03:54:30Z")

</div>

The node.js application establishes a persistent connection to Postgres for subscribing to [Postgres notifications events](https://www.postgresql.org/docs/current/sql-notify.html). It works fine but every 30 minutes `Connection terminated unexpectedly` error occurs.  
My question is, are there any platform time limits for persistent connection to Postgres after which the connection is closed?

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [June 16, 2022, 2:12pm UTC](https://community.fly.io/t/persistent-connection-to-postgres-database/5663/2 "2022-06-16T14:12:33Z")

</div>

The haproxy instance running with postgres has a 30 min idle timeout: [postgres-ha/haproxy.cfg at main · fly-apps/postgres-ha · GitHub](https://github.com/fly-apps/postgres-ha/blob/main/config/haproxy.cfg#L10)

Which Node driver are you using? Most of them have pool/reconnect settings you can use to handle connections dropping. It’s probably good practice to do anyway since timeouts are only one possible reason for a drop.

If you really want to avoid the proxy, you can connect to port 5433. But if you’re running multiple Postgres instances, you’ll need to teach your app how to connect to all of them and detect which one is writable. The haproxy handles that for you.

---

<div class="post-metadata">

**Author:** ![denishacks](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/denishacks/32/4056_2.png) [@denishacks](https://community.fly.io/u/denishacks)\
**Post date:** [June 16, 2022, 5:58pm UTC](https://community.fly.io/t/persistent-connection-to-postgres-database/5663/3 "2022-06-16T17:58:13Z")

</div>

I use the `node-postgres` client which is based on the `libpq` library. As I mentioned, it works fine because reconnection functionality was initially laid down. What was not clear to me is the reason for connection termination, but I get it now. Thank you for your exhaustive response.

---

<div class="post-metadata">

**Author:** ![pier](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/pier/32/74_2.png) [@pier](https://community.fly.io/u/pier)\
**Post date:** [June 17, 2022, 3:13am UTC](https://community.fly.io/t/persistent-connection-to-postgres-database/5663/4 "2022-06-17T03:13:14Z")

</div>

I’m also using Node with PG events.

I’m using this driver which I like a lot better than the usual `node-postgres`:

> **[GitHub - porsager/postgres: Postgres.js - The Fastest full featured...](https://github.com/porsager/postgres)**
>
> Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare - porsager/postgres

The only hiccup I had was getting connection closed error. [Here are the details.](https://github.com/porsager/postgres/issues/310)

This is how I solved it:

```auto
export const sql = postgres(process.env.DATABASE_URL, {
	idle_timeout: 20
});

```

The author of the driver suggested to use 120 seconds for the `idle_timeout`. That seemed excessive so I played a bit with that and 20 seconds has been working flawlessly with PG on Fly for weeks.

---

<div class="post-metadata">

**Author:** ![denishacks](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/denishacks/32/4056_2.png) [@denishacks](https://community.fly.io/u/denishacks)\
**Post date:** [June 24, 2022, 6:23am UTC](https://community.fly.io/t/persistent-connection-to-postgres-database/5663/5 "2022-06-24T06:23:50Z")

</div>

Thank you for the link. There is [good advice](https://github.com/porsager/postgres/issues/310#issuecomment-1094202695) in the discussion - ping the listener to prevent a connection from idle.
