# Migrate 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](/docs/migrate).

Heroku facts on this page come from Heroku's documentation, read on October 10, 2026: [importing and exporting Heroku Postgres databases](https://devcenter.heroku.com/articles/heroku-postgres-import-export), [logical replication support](https://help.heroku.com/TVS8OHTR/does-heroku-postgres-support-logical-replication), [the heroku_ext schema](https://help.heroku.com/ZOFBHJCJ/heroku-postgres-extension-changes-faq), [connecting to Heroku Postgres](https://devcenter.heroku.com/articles/connecting-heroku-postgres) and [dyno IP addresses and allowlists](https://help.heroku.com/JS13Y78I/i-need-to-add-heroku-dynos-to-our-allowlist-what-are-ip-address-ranges-in-use-at-heroku).

## Prepare

The commands below use the Heroku CLI and an app named `example-app`.

1. **Check the source's version, size and extensions:**

   ```bash
   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"
   ```

2. **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:

   ```bash
   export TARGET_URL="postgresql://shop_owner:<password>@k7m2x9qa4pz1.us-east.db.keeldb.com:5432/shop?sslmode=require"
   ```

3. **Install `pg_restore`** at least as new as the KeelDB server, usually PostgreSQL 18's.

4. **Recreate the extension schema if the source uses one.** Heroku creates a schema named `heroku_ext` in its databases, and from August 2022 to mid 2023 it installed extensions there. Column defaults then refer to that schema, for example `heroku_ext.uuid_generate_v4()`. If the query in step 1 lists extensions in `heroku_ext`, create the same schema on KeelDB and the trusted extensions in it before the restore:

   ```sql
   -- 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](/docs/extensions): turn on catalog extensions such as `vector` or `postgis` in the console instead.

## Dump

1. **Stop writes.** Put the app in maintenance mode and scale down any workers that write:

   ```bash
   heroku maintenance:on --app example-app
   ```

2. **Capture a backup and download it.** Heroku backups use `pg_dump`'s custom format, which `pg_restore` reads directly. Heroku suggests this for databases up to 20 GB:

   ```bash
   heroku pg:backups:capture --app example-app
   heroku pg:backups:download --app example-app
   ```

   This saves the backup as `latest.dump`.

   Above 20 GB, or if you prefer, run `pg_dump` yourself against `$SOURCE_URL` and restore that file instead of `latest.dump`:

   ```bash
   pg_dump "$SOURCE_URL" --format=custom --no-owner --no-acl --file=latest.dump
   ```

## Restore

```bash
pg_restore --dbname="$TARGET_URL" --no-owner --no-acl --jobs=4 latest.dump
psql "$TARGET_URL" -c "ANALYZE"
```

- `--no-owner --no-acl` leaves out ownership and grants that name Heroku's roles, so restored objects belong to your KeelDB owner role.
- `--jobs` runs that many connections at once. Keep it within your role's connection limit and around the database's vCPU count.
- `pg_restore` reports errors and carries on. An error about `heroku_ext` means a schema or extension from step 4 of [Prepare](#prepare) is missing; create it and restore again into a fresh database. A failed `CREATE TABLE` always needs a look.

## Verify

Compare row counts table by table, while Heroku is still read-only:

```bash
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:

  ```bash
  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_URL` to KeelDB's pooled string. Keep a backup of the Heroku database before you remove it.

Then:

1. Run migrations over KeelDB's **direct** host. Rails, for example: `DATABASE_URL="$TARGET_URL" bin/rails db:migrate`. Rails needs `prepared_statements: false` and `advisory_locks: false` on the pooled host; see [Connecting](/docs/connect#ruby-on-rails).
2. Make sure the database's [IP allowlist](/docs/security#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.
3. Turn maintenance mode off: `heroku maintenance:off --app example-app`.
4. 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.
