Create a Fly Postgres Cluster

To create a Postgres cluster, use the fly postgres create command. The command will walk you through the creation with prompts for name, region, and VM resources.

Because we keep adding shorter aliases, you can use any of the following and get the same result: flyctl postgres, fly postgres, flyctl pg, and fly pg.

fly postgres create
? Choose an app name (leave blank to generate one): pg-test
? Select Organization: TestOrg (personal)
Some regions require a paid plan (fra, maa).
See https://fly.io/plans to set up a plan.

? Select region: Toronto, Canada (yyz)

During this process, you get to choose from several preset resource configurations for the app:

? Select configuration:  [Use arrows to move, type to filter]
> Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
  Production (High Availability) - 3 nodes, 2x shared CPUs, 4GB RAM, 40GB disk
  Production (High Availability) - 3 nodes, 4x shared CPUs, 8GB RAM, 80GB disk
  Specify custom configuration

The “Production” options give you a three-node cluster in a leader-replica configuration. A single-node “Development” instance can readily be scaled and expanded to more regions.

If you select the “Development” single-node cluster configuration, then you can choose to scale down to zero if there are no open connections after one hour.

? Scale single node pg to zero after one hour? (y/N)

You might need to configure any apps that connect to your Postgres app to scale to zero as well, otherwise your Postgres database will never have zero connections and will never scale down. Your app might also need to be able to wait for the database to start back up. Learn more about the scale to zero feature, including how to turn it off.

Creating postgres cluster in organization TestOrg
Creating app...
Setting secrets on pg-test...
Provisioning 1 of 1 machines with image flyio/postgres-flex:15.2
Waiting for machine to start...
Machine 3287457df90185 is created
==> Monitoring health checks
  Waiting for 3287457df90185 to become healthy (started, 3/3)

Postgres cluster pg-test created
  Username:    postgres
  Password:    45V1YkwVDUzbkHj
  Hostname:    pg-test.internal
  Flycast:     fdaa:2:45b:0:1::7
  Proxy port:  5432
  Postgres port:  5433
  Connection string: postgres://postgres:45V1YkwVDUzbkHj@pg-test.flycast:5432

Save your credentials in a secure place -- you won't be able to see them again!

Connect to postgres
Any app within the TestOrg organization can connect to this Postgres using the above connection string

Now that you've set up Postgres, here's what you need to understand: https://fly.io/docs/postgres/getting-started/what-you-should-know/

After answering all the prompts, you’ll see a message saying that the cluster is being created. Take heed of the reminder to save your password in a safe place!

Once deployment is complete, you’ll be looking to connect an app to your new Postgres cluster. There’s a good chance you’ll want to attach a Fly app next.

Before going any further, a note: the automated Postgres creation process doesn’t generate a fly.toml file in the working directory. This means that when you use flyctl commands that would otherwise look for an app name in fly.toml, you’ll have to specify the app, like so:

flyctl <command> -a <postgres-app-name>