Re: Option on `postgres` CLI to shutdown when there are no more active connections?

Thiemo Kellner <thiemo@gelassene-pferde.biz>

From: Thiemo Kellner <thiemo@gelassene-pferde.biz>
To: pgsql-general@lists.postgresql.org
Date: 2025-10-15T05:47:34Z
Lists: pgsql-general
14.10.2025 23:49:21 David Barsky <me@davidbarsky.com>:

> 1. If that script is cancelled or interrupted for any reason, it’s possible that
>    `pg_ctl stop` won't be called and I'd have a leaked process.

As others stated, some shells have the trapping functionality.

> I could mitigate
>    this by calling `pg_ctl stop` at the *start* of the script, but that adds a
>    bit of latency I'd prefer to avoid.

I wonder what the reason is to avoid that latency in the script. Spinning down the DB will take time anyway.

Would it be possible to make a procedure run at the start of the DB, like a daemon, checking for the last connection to quit if there has been one, and to shutdown the DB in an ordered manner?

Just my two Rappen