Skip to main content
Important: We are not able to provide support or guidance for unmanaged Postgres. We now offer Fly.io Managed Postgres , our fully-managed database service that handles all aspects of running production PostgreSQL databases.
These examples demonstrate connections from within the same Fly.io internal IPv6 private network . To adapt them for external connections, substitute the internal hostname for one that’s reachable over the public Internet.

psql

If you have an active WireGuard tunnel to your organization’s private network, you can connect from a local machine to your Postgres cluster the same way you would from a Fly app within the same private network. For example, the following command would start an interactive terminal session on the cluster leader with psql:
You can, of course, run psql from a shell on a Fly app as well (as long as that app has psql installed).

Connecting with Ruby

docs Ruby apps use the pg gem to connect to postgres.

Connecting with Rails

docs Rails apps automatically connect to the database specified in the DATABASE_URL environment variable. You can set this variable manually with flyctl secrets set
or by attaching the Postgres database to your Fly app.

Connecting with Python

psycopg is the recommended driver for connecting to postgres:

Connecting with Django

docs psycopg is the recommended driver for connecting to postgres. You can use the django-environ package to translate the DATABASE_URL environment variable into Django database configuration. First, install both packages:
Now we can define DATABASES in our settings.py file.
You can set the DATABASE_URL manually with flyctl secrets set:
or by adding it to the .env file:

Connecting with Go

docs pgx is the recommended driver for connecting to postgres. It supports the standard database/sql interface as well as directly exposing low level / high performance APIs. First, add github.com/jackc/pgx/v4 as a module dependency.
The following program will connect to the database in DATABASE_URL and run a query.

Connecting with Node.js

docs You’ll use the pg npm module to connect to Postgres from a Node.js app.

Connecting with Prisma – Node.js

docs Prisma is an open-source object-relational mapper (ORM) for Node.js and works with both JavaScript and TypeScript. It consists of 3 components:
  • Prisma Client - a type-safe query builder
  • Prisma Migrate - a data modeling and migration tool
  • Prisma Studio - a modern intuitive GUI for interacting with your database
Install the Prisma CLI and Prisma Client dependencies in your project
Initialize Prisma in your project:
This command does the following:
  • Creates a folder called prisma at the root of your project
  • Creates a .env file at the root of your project if it doesn’t exist
  • Creates a schema.prisma file inside the prisma folder. This is the file that you will use to model your data
Update the DATABASE_URL in the .env to your PostgreSQL database
If you are working in a brownfield project, you can introspect your database to generate the models in your schema.prisma file:
Assuming you have the following model in your schema.prisma file: Add a model to your schema.prisma file:
You can query your database using Prisma as follows:

Connecting with Sequelize – Node.js

docs Sequelize is an open-source object-relational mapper (ORM) for Node.js and works with JavaScript and TypeScript.
Install the Sequelize dependencies and CLI into your project
Initialize Sequelize into your project:
This will create the following folders:
  • config, contains config file, which tells CLI how to connect with database
  • models, contains all models for your project
  • migrations, contains all migration files
  • seeders, contains all seed files
Docs: Create your first model
After initializing you will need to show the CLI how to connect to the database inside of config/config.js
Create a new file named .sequelizerc , this allows the CLI to be able to read your config.js file
Next pass the DATABASE_URL from the env into the Sequelize constructor in models/index.js to be able to connect to your fly postgres app and confirm the new Sequelize connection with the async function below
Fly docs: How to add Read Replica database with flyctlSet the Sequelize constructor in model/index.js to conditionally connect to the primary/read-replica database depending on the primary region value set in fly.toml.