DocsGuides
View as MarkdownMigrate from Heroku Postgres
Move a Heroku Postgres database to KeelDB with a Heroku backup and pg_restore, handle the heroku_ext schema, and point your Heroku app at KeelDB.
This guide moves a Heroku Postgres database to KeelDB with a dump and restore. Heroku Postgres does not offer logical replication to databases outside Heroku, so writes stop for as long as the dump and restore take; plan a maintenance window that fits the size of your data. The general steps are in Migrate to KeelDB.
Heroku facts on this page come from Heroku's documentation, read on October 10, 2026: importing and exporting Heroku Postgres databases, logical replication support, the heroku_ext schema, connecting to Heroku Postgres and dyno IP addresses and allowlists.
Prepare
The commands below use the Heroku CLI and an app named example-app.
-
Check the source's version, size and extensions:
export SOURCE_URL="$(heroku config:get DATABASE_URL --app example-app)" psql "$SOURCE_URL" -c "SHOW server_version" psql "$SOURCE_URL" -c "SELECT pg_size_pretty(pg_database_size(current_database()))" psql "$SOURCE_URL" -c "SELECT e.extname, n.nspname AS schema FROM pg_extension e JOIN pg_namespace n ON n.oid = e.extnamespace ORDER BY 1" -
Create the KeelDB database with a PostgreSQL version at least as new as the source's (KeelDB runs 18, 17 and 16) and a plan whose storage leaves room to grow. Wait until it is
ready, then copy its direct connection string from the Overview page:export TARGET_URL="postgresql://shop_owner:<password>@k7m2x9qa4pz1.us-east.db.keeldb.com:5432/shop?sslmode=require" -
Install
pg_restoreat least as new as the KeelDB server, usually PostgreSQL 18's. -
Recreate the extension schema if the source uses one. Heroku creates a schema named
heroku_extin its databases, and from August 2022 to mid 2023 it installed extensions there. Column defaults then refer to that schema, for exampleheroku_ext.uuid_generate_v4(). If the query in step 1 lists extensions inheroku_ext, create the same schema on KeelDB and the trusted extensions in it before the restore:-- On KeelDB CREATE SCHEMA IF NOT EXISTS heroku_ext; CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA heroku_ext; CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA heroku_ext;Create only the ones your database uses. Check each against Extensions: turn on catalog extensions such as
vectororpostgisin the console instead.
Dump
-
Stop writes. Put the app in maintenance mode and scale down any workers that write:
heroku maintenance:on --app example-app -
Capture a backup and download it. Heroku backups use
pg_dump's custom format, whichpg_restorereads directly. Heroku suggests this for databases up to 20 GB:heroku pg:backups:capture --app example-app heroku pg:backups:download --app example-appThis saves the backup as
latest.dump.Above 20 GB, or if you prefer, run
pg_dumpyourself against$SOURCE_URLand restore that file instead oflatest.dump:pg_dump "$SOURCE_URL" --format=custom --no-owner --no-acl --file=latest.dump
Restore
pg_restore --dbname="$TARGET_URL" --no-owner --no-acl --jobs=4 latest.dump
psql "$TARGET_URL" -c "ANALYZE"--no-owner --no-aclleaves out ownership and grants that name Heroku's roles, so restored objects belong to your KeelDB owner role.--jobsruns that many connections at once. Keep it within your role's connection limit and around the database's vCPU count.pg_restorereports errors and carries on. An error aboutheroku_extmeans a schema or extension from step 4 of Prepare is missing; create it and restore again into a fresh database. A failedCREATE TABLEalways needs a look.
Verify
Compare row counts table by table, while Heroku is still read-only:
psql "$SOURCE_URL" -At -c "
SELECT format('SELECT %L, count(*) FROM %I.%I;', schemaname || '.' || relname, schemaname, relname)
FROM pg_stat_user_tables
ORDER BY 1" > count-queries.sql
psql "$SOURCE_URL" -At -F ' ' -f count-queries.sql > source-counts.txt
psql "$TARGET_URL" -At -F ' ' -f count-queries.sql > target-counts.txt
diff source-counts.txt target-counts.txt && echo "Row counts match"Point your app at KeelDB
Heroku sets DATABASE_URL from the attached Heroku Postgres add-on and rewrites it on failovers and credential rotations, so a value you put there yourself does not stay. Two ways to switch:
-
Keep the add-on for now. Store KeelDB's pooled connection string in a new variable and read it in your app:
heroku config:set KEELDB_DATABASE_URL="postgresql://shop_owner:<password>@k7m2x9qa4pz1-pooler.us-east.db.keeldb.com:5432/shop?sslmode=require&connect_timeout=10" --app example-app -
Detach the add-on once you no longer need it, then set
DATABASE_URLto KeelDB's pooled string. Keep a backup of the Heroku database before you remove it.
Then:
- Run migrations over KeelDB's direct host. Rails, for example:
DATABASE_URL="$TARGET_URL" bin/rails db:migrate. Rails needsprepared_statements: falseandadvisory_locks: falseon the pooled host; see Connecting. - Make sure the database's IP allowlist allows your Heroku dynos, or leave it empty. Heroku describes the addresses of Common Runtime dynos as highly dynamic; to allowlist them, give the app a static outbound IP add-on, or use the static outbound IPs of a Private Space.
- Turn maintenance mode off:
heroku maintenance:off --app example-app. - Take an on-demand backup on KeelDB (Back up now).
Keep the Heroku database until you are confident, then remove it and rotate any credentials that were shared with it.