Attach or Detach a Fly App

Attach a Fly App

fly postgres attach is a convenience command to quickly associate a Fly App with your Fly Postgres cluster app.

fly postgres attach <postgres app name> --app <app name>

When you attach an app to Postgres, a number of things happen:

  • A new database and user are created in the Postgres cluster app, using the name of the consuming app (a Fly App). If the consuming app is named “my-app”, then both the database and the user are named “my_app”.
  • The user is allocated a generated password.
  • The consuming app and the Postgres app are marked as attached in the great floating ledger in the Fly.io cloud, which enables you to use fly postgres detach later.

When the now-attached consuming app starts, it will have access to an environment variable DATABASE_URL set to a Postgres connection URI with the username, password, host, port, and dbname filled in.

Detach a Fly App

If the consuming app’s access to this database was purely due to fly postgres attach, then you can revoke that access using fly postgres detach.

fly postgres detach <postgres app name> --app <app name>

This will remove the <app name> user from the Postgres cluster, and the DATABASE_URL secret from the <app name> app. The platform will no longer view the two apps as associated. The database is not removed.

Check if a Fly App is attached to a Postgres app

Run fly postgres users list --app <postgres app name> on your Postgres cluster app. If there is an attached Fly App, then the app’s name will be in the list of users. See Users & Roles.