# Just because it's cool - how does the Scale to zero Postgres for hobby projects work?

**URL:** <https://community.fly.io/t/just-because-its-cool-how-does-the-scale-to-zero-postgres-for-hobby-projects-work/15020>\
**Category:** General\
**Tags:** login-only\
**Created:** [August 25, 2023, 10:43am UTC](https://community.fly.io/t/just-because-its-cool-how-does-the-scale-to-zero-postgres-for-hobby-projects-work/15020 "2023-08-25T10:43:46Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![MatthewIngwersen](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@MatthewIngwersen](https://community.fly.io/u/MatthewIngwersen)\
**Post date:** [August 28, 2023, 4:04pm UTC](https://community.fly.io/t/just-because-its-cool-how-does-the-scale-to-zero-postgres-for-hobby-projects-work/15020/2 "2023-08-28T16:04:32Z")

</div>

Hey @mrchrisadams! Fly Postgres is [open source](https://github.com/fly-apps/postgres-flex), and you can find the scale-to-zero implementation [here](https://github.com/fly-apps/postgres-flex/blob/master/cmd/start/main.go#L130).

It uses the same method described in the Elixir blog post that you linked. When scale-to-zero is enabled (via an environment variable), the supervisor process (`start`) periodically checks whether there are any open Postgres client connections. If not, then it stops Postgres and exits. When the Machine’s main process (`start`) exits, the Machine shuts down.

On the `flyctl` side, when the Postgres Machine is created, it’s [configured not to auto-restart](https://github.com/superfly/flyctl/blob/5da4a19cb85f27ea3153b6979bde1612c89b75eb/flypg/launcher.go#L348) unless it fails. Therefore, when the Machine shuts itself down, it stays down.

(It’s also worth noting that since scale-to-zero for PG was introduced, we added [support](https://community.fly.io/t/automatically-starting-stopping-apps-v2-instances/12342) for using our edge proxy to auto-stop machines, so depending on the use-case you may not need to program this logic into the app itself anymore.)

Hope this helps!

---

_[View the full topic](https://community.fly.io/t/just-because-its-cool-how-does-the-scale-to-zero-postgres-for-hobby-projects-work/15020)._
