On Fly.io​

In this guide we show how to deploy EdgeDB to Fly.io using a Fly.io PostgreSQL cluster as the backend.

Prerequisites​

  • Fly.io account

  • flyctl CLI (install)

Provision a Fly.io app for EdgeDB​

Every Fly.io app must have a globally unique name, including service VMs like Postgres and EdgeDB. Here we assume the name for the EdgeDB app is “myorg-edgedb”, which you would need to replace with a name of your choosing.

  1. EDB_APP=myorg-edgedb
  1. flyctl apps create --name $EDB_APP
  1. New app created: myorg-edgedb

Now, let’s secure the pending EdgeDB instance with a strong password:

  1. read -s EDGEDB_PASSWORD
  1. <enter-password>
  1. flyctl secrets set EDGEDB_PASSWORD="$EDGEDB_PASSWORD" -a $EDB_APP
  1. Secrets are staged for the first deployment

There are a couple more environment variables we need to set:

  1. flyctl secrets set \
  2. EDGEDB_SERVER_BACKEND_DSN_ENV=DATABASE_URL \
  3. EDGEDB_SERVER_TLS_CERT_MODE=generate_self_signed \
  4. -a $EDB_APP
  1. Secrets are staged for the first deployment

The EDGEDB_SERVER_BACKEND_DSN_ENV tells the EdgeDB container where to look for the PostgreSQL connection string (more on that below), and the EDGEDB_SERVER_TLS_CERT_MODE tells EdgeDB to auto-generate a self-signed TLS certificate. You may choose to provision a custom TLS certificate instead and pass it in the EDGEDB_SERVER_TLS_CERT secret, with the private key in the EDGEDB_SERVER_TLS_KEY secret.

Finally, let’s scale the VM as EdgeDB requires a little bit more than the default Fly.io VM side provides:

  1. flyctl scale vm dedicated-cpu-1x -a $EDB_APP
  1. Scaled VM Type to
  2. dedicated-cpu-1x
  3. CPU Cores: 1
  4. Memory: 2 GB

Create a PostgreSQL cluster​

Now we need to provision a PostgreSQL cluster and attach it to the EdgeDB app.

If you have an existing PostgreSQL cluster in your Fly.io organization, you can skip to the attachment step.

Create a new PostgreSQL cluster:

  1. PG_APP=myorg-postgres
  1. flyctl pg create --name $PG_APP --vm-size dedicated-cpu-1x
  1. ? Select VM size: dedicated-cpu-1x - 256
  2. ? Volume size (GB): 10
  3. Creating postgres cluster myorg-postgres in organization personal
  4. Postgres cluster myorg-postgres created
  5. ...
  6. --> v0 deployed successfully

Attach the PostgreSQL cluster to the EdgeDB app:

  1. PG_ROLE=myorg_edgedb
  1. flyctl pg attach \
  2. --postgres-app "$PG_APP" \
  3. --database-user "$PG_ROLE" \
  4. -a $EDB_APP
  1. Postgres cluster myorg-postgres is now attached to myorg-edgedb
  2. The following secret was added to myorg-edgedb:
  3. DATABASE_URL=postgres://...

When you deploy EdgeDB it will now automatically recognize which PostgreSQL cluster to run on (via the EDGEDB_SERVER_BACKEND_DSN_ENV = "DATABASE_URL" bit we added in an earlier step).

Lastly, EdgeDB needs the ability to create Postgres databases and roles, so let’s adjust the permissions on the role that EdgeDB will use to connect to Postgres:

  1. echo "alter role \"$PG_ROLE\" createrole createdb; \quit" \
  2. | flyctl pg connect $PG_APP
  1. ...
  2. ALTER ROLE

Start EdgeDB​

Everything is set, time to start EdgeDB:

  1. flyctl deploy --image=edgedb/edgedb:nightly \
  2. --remote-only -a $EDB_APP
  1. ...
  2. 1 desired, 1 placed, 1 healthy, 0 unhealthy
  3. --> v0 deployed successfully

That’s it! You can now start using the EdgeDB instance located at edgedb://myorg-edgedb.internal/ in your Fly.io apps.

If deploy did not succeed, make sure you’ve scaled the EdgeDB VM appropriately and check the logs (flyctl logs myorg-edgedb).

Persist the generated TLS certificate​

Now we need to persist the auto-generated TLS certificate to make sure it survives EdgeDB app restarts. (If you’ve provided your own certificate, skip this step).

  1. EDB_SECRETS="EDGEDB_SERVER_TLS_KEY EDGEDB_SERVER_TLS_CERT"
  1. flyctl ssh console -a $EDB_APP -C \
  2. "edgedb-show-secrets.sh --format=toml $EDB_SECRETS" \
  3. | tr -d '\r' | flyctl secrets import -a $EDB_APP

To access the EdgeDB instance you’ve just provisioned on Fly.io from your local machine first make sure you have the Private Network VPN up and running and then run edgedb instance link:

  1. echo $EDGEDB_PASSWORD | edgedb instance link \
  2. --trust-tls-cert \
  3. --host $EDB_APP.internal \
  4. --password-from-stdin \
  5. --non-interactive \
  6. fly
  1. Authenticating to edgedb://edgedb@myorg-edgedb.internal:5656/edgedb
  2. Successfully linked to remote instance. To connect run:
  3. edgedb -I fly

Don’t forget to replace myorg-edgedb above with the name of your EdgeDB app. You can now use the EdgeDB instance deployed on Fly.io as fly, for example:

  1. edgedb -I fly
  1. edgedb>