How to dump a Fly.io PostgreSQL database locally
By Daniel Tello · Updated December 2, 2026
To dump a Fly.io PostgreSQL database locally, open a WireGuard tunnel with fly mpg proxy <cluster-id> for Managed Postgres (port 16380) or fly proxy 15432:5432 -a <db-app> for unmanaged Fly Postgres. Then run pg_dump against 127.0.0.1 in a second terminal and restore locally with pg_restore --no-owner --no-acl.
Managed Postgres vs. unmanaged Fly Postgres
Fly.io databases sit inside your organization's private WireGuard network and are not exposed to the public internet by default. Before running pg_dump, check which Postgres setup you run because the proxy command and default local port differ:
| Setup | Proxy command | Default local endpoint | Where credentials live |
|---|---|---|---|
| Fly Managed Postgres (MPG) | fly mpg proxy <cluster-id> |
127.0.0.1:16380 |
Fly Dashboard (cluster Connect tab) or app DATABASE_URL |
| Unmanaged Fly Postgres app | fly proxy 15432:5432 -a <db-app> |
127.0.0.1:15432 |
Initial creation output or app DATABASE_URL |
Both proxy commands bind to IPv4 127.0.0.1 unless you pass --bind-addr. Using 127.0.0.1 instead of localhost in your pg_dump command avoids IPv6 resolution mismatches on macOS and Linux.
What you need before starting
flyctlinstalled and authenticated against the Fly.io organization that owns the database.- Local PostgreSQL client tools (
pg_dump,pg_restore,psql, andcreatedb). Check your version withpg_dump --version: PostgreSQL requires a localpg_dumpmajor version equal to or higher than the remote server (for example, Managed Postgres runs Postgres 16, 17, or 18). - Your database name, username, and password (or full connection string). Fly.io secrets cannot be read back with
fly secrets list; to retrieve credentials already attached to an app Machine, inspect the app environment or open the Managed Postgres Connect tab in the Fly Dashboard.
Step 1: Open a local proxy tunnel
Keep this terminal tab open until the dump finishes. Closing it terminates the WireGuard tunnel immediately.
Option A: Fly Managed Postgres (MPG)
Per the fly mpg proxy reference, pass your cluster ID (or omit it in an interactive terminal to pick an organization and cluster from a list):
fly mpg proxy <cluster-id>
By default this listens on 127.0.0.1:16380. If another local process already uses port 16380, override it with --local-port:
fly mpg proxy <cluster-id> --local-port 16381
Option B: Unmanaged Fly Postgres app
Per the fly proxy reference, forward remote port 5432 on your database app to a free local port such as 15432 (so it does not collide with a local PostgreSQL server on 5432):
fly proxy 15432:5432 -a your-app-db
If your app has multiple Machines and you want to target a specific instance (for example, a read replica instead of the primary), add --select to choose the Machine interactively.
Step 2: Dump the database with pg_dump
Open a second terminal window and run pg_dump against the proxied port. You have two practical output formats in the PostgreSQL pg_dump documentation: custom archive (-Fc) or plain-text SQL.
Recommended: custom archive format (-Fc)
The custom format is compressed by default, transfers faster over a remote proxy, and lets pg_restore reorder items or restore tables selectively in parallel. For Managed Postgres using the proxied port 16380:
pg_dump -h 127.0.0.1 -p 16380 -U fly-user -d your_database_name -Fc -f dump.backup
For an unmanaged Fly Postgres app proxied on port 15432:
pg_dump -h 127.0.0.1 -p 15432 -U postgres -d your_database_name -Fc -f dump.backup
PostgreSQL prompts for the database password unless you supply it via a connection URI or PGPASSWORD. Note an easy trap documented in the pg_dump manual: flags like --no-owner and --no-acl are ignored when emitting a non-text archive such as -Fc. You pass those flags to pg_restore in Step 4 instead.
Alternative: plain-text SQL dump
For small databases where you want a readable SQL file you can inspect or edit before restoring, omit -Fc and include --no-owner --no-acl right on pg_dump so Fly.io roles do not appear in the SQL script:
pg_dump -h 127.0.0.1 -p 15432 -U postgres -d your_database_name --no-owner --no-acl -f dump.sql
Step 3: Close the proxy
Once pg_dump exits cleanly and you confirm the output file is non-empty (ls -lh dump.backup), switch back to the first terminal and press Ctrl+C to close the WireGuard tunnel. Closing it immediately keeps you from accidentally pointing a local migration or restore command at production.
Step 4: Restore the dump locally
Make sure your local PostgreSQL server is running, then create a clean target database if it does not exist yet:
createdb -h 127.0.0.1 -U postgres your_local_database
Restoring a custom archive (-Fc) with pg_restore
Per the PostgreSQL pg_restore documentation, pass --no-owner and --no-acl so every restored table and sequence belongs to your local user instead of failing on Fly-only roles:
pg_restore -h 127.0.0.1 -U postgres -d your_local_database --no-owner --no-acl dump.backup
If you are refreshing an existing local database rather than loading into a brand-new one, add --clean --if-exists so pg_restore drops existing objects before recreating them without reporting errors on missing ones. Double-check the host and database name before running --clean so it only ever hits your local machine:
pg_restore -h 127.0.0.1 -U postgres -d your_local_database --no-owner --no-acl --clean --if-exists dump.backup
Restoring a plain-text SQL file with psql
If you generated a plain dump.sql file, load it with psql:
psql -h 127.0.0.1 -U postgres -d your_local_database -f dump.sql
Common errors and how to fix them
pg_dump: error: aborting because of server version mismatch
Your local pg_dump binary is older than the PostgreSQL major version running on Fly.io. Run pg_dump --version to verify. On macOS with Homebrew, install the matching client (for example brew install libpq or postgresql@17) and call that version's pg_dump binary directly.
Connection refused on localhost while the proxy is open
Both fly proxy and fly mpg proxy bind to IPv4 127.0.0.1 by default. On some machines localhost resolves to IPv6 ::1 first, which fails with connection refused. Replace -h localhost with -h 127.0.0.1.
role "fly-user" or "postgres" does not exist during restore
PostgreSQL throws a role-not-found error on restore when a dump includes ALTER OWNER or GRANT statements for Fly.io roles that do not exist on your local server. Re-run pg_restore with --no-owner --no-acl (or regenerate a plain SQL dump with those two flags).
Address already in use when starting the proxy
Another process (often an earlier proxy tab or a local container) is bound to the local port. Pick any unused local port: fly proxy 15433:5432 -a your-app-db or fly mpg proxy <cluster-id> --local-port 16381, then pass that same port to pg_dump -p.
When to use Fly.io snapshots instead of pg_dump
pg_dump is built for pulling a local copy for debugging, seeding development, or migrating across major Postgres versions. For routine disaster recovery on production, Managed Postgres includes automated backups and recovery out of the box, and unmanaged Fly Postgres supports volume snapshots documented in Fly.io's backup and restore guide. If you work with databases in CI or production Ruby apps, you may also find my notes on Rails GitHub Actions with MySQL, running Sidekiq on Heroku, and the rest of the developer blog useful.
Frequently asked questions
Should I use fly mpg proxy or fly proxy to dump a Fly.io Postgres database?
Use fly mpg proxy if your database runs on Fly.io Managed Postgres (it listens on 127.0.0.1:16380 by default). Use fly proxy 15432:5432 -a your-app-db if you run an older unmanaged Fly Postgres app inside your own Machines.
Why did pg_restore fail with role does not exist after pg_dump -Fc?
In PostgreSQL custom archive format (-Fc), pg_dump ignores --no-owner and --no-acl and keeps role metadata inside the archive. Pass --no-owner --no-acl to pg_restore when loading the dump locally so your local PostgreSQL user owns the restored objects.
Why does pg_dump fail with server version mismatch?
pg_dump refuses to dump from a PostgreSQL server running a newer major version than your local pg_dump binary. Check your local client with pg_dump --version and install a PostgreSQL client whose major version matches or exceeds the Fly.io cluster.