Skip to content
Menu

DocsGuides

View as Markdown

Migrate to KeelDB

Move an existing PostgreSQL database to KeelDB with pg_dump and pg_restore, or with logical replication for near-zero downtime.

This guide uses only standard PostgreSQL tools, so it applies to any source you can reach with pg_dump, such as Neon, Supabase, Heroku Postgres or Amazon RDS. Check your current provider's documentation for how to get a direct connection string and, for logical replication, how to enable it.

Two approaches:

ApproachDowntimeBest for
Dump and restoreWrites stop for as long as the dump and restore takeMost databases, especially up to a few tens of GB
Logical replicationSeconds to minutes at cutoverLarge or busy databases that can't stop for long

Prepare

The commands below use two variables: the source's direct connection string and your KeelDB database's direct URI.

export SOURCE_URL="postgresql://user:<password>@source-host:5432/app?sslmode=require"
export TARGET_URL="postgresql://shop_owner:<password>@k7m2x9qa4pz1.us-east.db.keeldb.com:5432/shop?sslmode=require"
  1. Check the source version. KeelDB runs PostgreSQL 18, 17 and 16. Pick a version at least as new as the source's major version.

    psql "$SOURCE_URL" -c "SHOW server_version"
  2. Size the plan. Check the source's size and pick a plan whose storage leaves room to grow: Free holds 1 GB, Launch 100 GB, and each Dedicated size has its own maximum. See Plans and billing.

    psql "$SOURCE_URL" -c "SELECT pg_size_pretty(pg_database_size(current_database()))"
  3. Create the KeelDB database in the console or with the API, and wait until it is ready.

  4. Use direct connections on both sides. Use the source's direct (non-pooled) connection string if it has more than one, and KeelDB's direct host (<id>.<region>.db.keeldb.com), never the -pooler host, for dumps, restores and replication.

  5. Use client tools at least as new as the newer server. New KeelDB databases run PostgreSQL 18 unless you pick another version, so that usually means pg_dump and pg_restore from PostgreSQL 18. Older tools refuse to dump a newer server.

  6. Check extensions. Your KeelDB role owns the database but is not a superuser, so statements that need a superuser fail, including creating extensions that aren't marked trusted. List what the source uses:

    psql "$SOURCE_URL" -c "SELECT extname, extversion FROM pg_extension ORDER BY 1"

    Then check them against Extensions: create the trusted ones yourself before the restore, and turn on catalog extensions such as vector or postgis in the console first.

Dump and restore

1. Stop writes

Put the app in maintenance mode or stop the processes that write to the source, so nothing changes after the dump starts.

2. Dump the source

Custom format is compressed and lets pg_restore work in parallel. --no-owner --no-acl leaves out ownership and grants that refer to roles that don't exist on KeelDB, so restored objects belong to your _owner role.

pg_dump "$SOURCE_URL" --format=custom --no-owner --no-acl --file=app.dump

For large databases, the directory format can also dump in parallel:

pg_dump "$SOURCE_URL" --format=directory --jobs=4 --no-owner --no-acl --file=app.dir

If the source has schemas that belong to the platform rather than your app, leave them out with --exclude-schema=<name>.

3. Restore into KeelDB

pg_restore --dbname="$TARGET_URL" --no-owner --no-acl --jobs=4 app.dump
  • --jobs runs that many connections in parallel. Keep it within your role's connection limit (95 on Free) and around the database's vCPU count.
  • pg_restore reports errors and carries on. Read them: errors about extensions or objects owned by the source platform are often harmless, while a failed CREATE TABLE is not.

4. Update statistics

psql "$TARGET_URL" -c "ANALYZE"

Then verify and cut over.

Logical replication for near-zero downtime

Logical replication copies the existing data, then streams every change from the source until you switch over. Writes only stop for the final catch-up.

Before you start

  • Source: logical replication must be enabled (wal_level = logical), and you need a role allowed to replicate. How to do this depends on your current provider.
  • Network: the subscription connects from your KeelDB database to the source over the internet with TLS. The source must accept that connection.
  • KeelDB: your database's owner role is a member of pg_create_subscription, so it can create the subscription. No support ticket needed.
  • Every replicated table needs a primary key, or REPLICA IDENTITY FULL, for updates and deletes to replicate.
  • Logical replication does not copy schema changes, sequence values or large objects. Freeze schema changes during the migration and sync sequences at cutover.

1. Copy the schema

pg_dump "$SOURCE_URL" --schema-only --no-owner --no-acl --no-publications --no-subscriptions --file=schema.sql
psql "$TARGET_URL" -f schema.sql

2. Create a publication on the source

-- On the source
CREATE PUBLICATION keeldb_migration FOR ALL TABLES;

FOR ALL TABLES needs a superuser on the source. Without one, list the tables: CREATE PUBLICATION keeldb_migration FOR TABLE public.orders, public.customers;

3. Create the subscription on KeelDB

-- On KeelDB, connected to the direct host
CREATE SUBSCRIPTION keeldb_migration
  CONNECTION 'host=source-host port=5432 dbname=app user=replicator password=<password> sslmode=require'
  PUBLICATION keeldb_migration;

This creates a replication slot on the source, copies every table, then streams changes.

4. Watch it catch up

On KeelDB, every table should reach state r (ready):

SELECT srrelid::regclass AS table_name, srsubstate AS state
FROM pg_subscription_rel
ORDER BY 1;

On the source, the lag should approach zero:

SELECT slot_name, active,
       pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) AS lag
FROM pg_replication_slots
WHERE slot_name = 'keeldb_migration';

5. Cut over

  1. Stop writes to the source.

  2. Wait until the lag is zero.

  3. Copy sequence values, which logical replication doesn't carry:

    psql "$SOURCE_URL" -At -c "
      SELECT format('SELECT setval(%L, %s, true);', quote_ident(schemaname) || '.' || quote_ident(sequencename), last_value)
      FROM pg_sequences
      WHERE last_value IS NOT NULL" > sequences.sql
    psql "$TARGET_URL" -f sequences.sql
  4. Verify the data.

  5. Remove the subscription on KeelDB. This also drops the replication slot on the source while it is reachable:

    DROP SUBSCRIPTION keeldb_migration;
  6. Point your app at KeelDB, then drop the publication on the source: DROP PUBLICATION keeldb_migration;

A replication slot left behind on the source keeps WAL from being removed there. Make sure it is gone.

Verify

Compare row counts table by table:

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"

Also check that:

  • Every extension you need exists on KeelDB (SELECT extname FROM pg_extension).
  • Sequences continue after the source's highest values, so new inserts don't collide.
  • Your app's test suite passes against KeelDB. A branch of the migrated database is a safe place to run it.

Cutover checklist

  • KeelDB plan chosen with enough max storage and connections.
  • Extensions available and restore errors reviewed.
  • IP allowlist includes your app's outbound addresses, or is empty.
  • App configuration ready: pooled URI for app traffic, direct URI for migrations. See Connecting.
  • Writes stopped on the source; final restore or replication catch-up done.
  • Sequences synced (logical replication only).
  • Row counts verified.
  • App switched to KeelDB and deployed; errors, metrics and insights checked.
  • On-demand backup taken on KeelDB (Back up now).
  • Source kept read-only until you are confident, then decommissioned; replication slot and publication dropped; source credentials rotated.