# Migrate from Neon

Move a Neon database to KeelDB with pg_dump and pg_restore, or with logical replication for a cutover of seconds, including Neon's own rules for direct hosts and replication.

This guide moves one Neon branch, usually your production branch, to a KeelDB database with standard PostgreSQL tools. Recreate preview branches on KeelDB afterwards: they are instant on Free and Launch. The general steps are in [Migrate to KeelDB](/docs/migrate); this page adds what is specific to Neon.

| Approach | Downtime | Best for |
| --- | --- | --- |
| [Dump and restore](#dump-and-restore) | Writes stop for as long as the dump and restore take | Most databases, especially up to a few tens of GB |
| [Logical replication](#logical-replication) | Seconds to minutes at cutover | Large or busy databases that can't stop for long |

Neon facts on this page come from Neon's documentation, read on October 10, 2026: [connection pooling](https://neon.com/docs/connect/connection-pooling) and [logical replication](https://neon.com/docs/guides/logical-replication-neon). Check them again before you start, since Neon may change them.

## Prepare

### Get Neon's direct connection string

Neon gives each branch a pooled and a direct connection string. The pooled one has `-pooler` in its hostname, for example `ep-cool-darkness-123456-pooler.us-east-2.aws.neon.tech`; the direct one doesn't. The pooler runs PgBouncer in transaction mode, so use the direct string for `pg_dump` and for replication. In Neon's **Connect** dialog, turn connection pooling off and copy the string.

```bash
export SOURCE_URL="postgresql://<role>:<password>@ep-cool-darkness-123456.us-east-2.aws.neon.tech/neondb?sslmode=require"
```

### Create the KeelDB database

1. Check the source's version and size:

   ```bash
   psql "$SOURCE_URL" -c "SHOW server_version"
   psql "$SOURCE_URL" -c "SELECT pg_size_pretty(pg_database_size(current_database()))"
   ```

2. Create a KeelDB database with a PostgreSQL version at least as new as the source's (KeelDB runs 18, 17 and 16; 18 is the default) and a plan with room to grow. Storage limits are on [Plans and billing](/docs/plans-and-billing).
3. 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"
   ```

Use `pg_dump` and `pg_restore` at least as new as the newer of the two servers, usually PostgreSQL 18's. Older tools refuse to dump a newer server.

### Check extensions

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

Compare the list with [Extensions](/docs/extensions). Create trusted extensions such as `pg_trgm` or `citext` yourself before the restore, and turn on catalog extensions such as `vector` or `postgis` in the console first. Your KeelDB role owns its database but is not a superuser, so an extension that only exists on Neon is reported as an error by the restore; decide whether your app needs it.

## Dump and restore

1. **Stop writes** to Neon: put the app in maintenance mode or stop the processes that write.
2. **Dump** over the direct host. `--no-owner --no-acl` leaves out ownership and grants that name Neon's roles, so restored objects belong to your KeelDB owner role:

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

3. **Restore** into KeelDB over its direct host:

   ```bash
   pg_restore --dbname="$TARGET_URL" --no-owner --no-acl --jobs=4 app.dump
   ```

   Keep `--jobs` within your role's connection limit and around the database's vCPU count. Read the errors `pg_restore` prints: one about an extension that exists only on Neon is often harmless, a failed `CREATE TABLE` is not.

4. **Update statistics:**

   ```bash
   psql "$TARGET_URL" -c "ANALYZE"
   ```

Then [verify](#verify) and [cut over](#cut-over).

## Logical replication

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

### Neon's rules

- **Turning it on is permanent.** In the Neon Console, open the project's **Settings**, then **Postgres**, then **Logical replication**, and select **Enable**. This changes `wal_level` from `replica` to `logical`, and Neon says it cannot be reverted.
- **It restarts computes.** Enabling it restarts all of the project's computes and drops active connections. Pick a quiet moment.
- **Use a role with replication rights.** Roles created in the Neon Console, CLI or API belong to `neon_superuser`, which has the `REPLICATION` privilege. Roles created with SQL don't, and it can't be granted to them.
- **The Neon compute stays awake** while a subscriber is connected, so it does not scale to zero during the migration. Expect Neon compute usage for those days.
- **Don't leave the slot idle.** Neon removes inactive replication slots after about 40 hours. If the subscription is stopped longer than that, start again from the schema copy.

### KeelDB's side

- Your owner role is a member of `pg_create_subscription`, so it can create the subscription itself.
- The subscription connects from KeelDB to Neon's direct host over the internet with TLS.
- 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 until the cutover.

### Steps

1. **Copy the schema** to KeelDB:

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

   ```sql
   -- On Neon
   CREATE PUBLICATION keeldb_migration FOR ALL TABLES;
   ```

   If that is refused for lack of privileges, list the tables instead: `CREATE PUBLICATION keeldb_migration FOR TABLE public.orders, public.customers;`

3. **Create the subscription on KeelDB** with Neon's direct hostname and a role that has replication rights:

   ```sql
   -- On KeelDB, connected to the direct host
   CREATE SUBSCRIPTION keeldb_migration
     CONNECTION 'host=ep-cool-darkness-123456.us-east-2.aws.neon.tech port=5432 dbname=neondb user=<role> password=<password> sslmode=require'
     PUBLICATION keeldb_migration;
   ```

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

4. **Watch it catch up.** On KeelDB, every table should reach state `r` (ready):

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

   On Neon, the lag should approach zero:

   ```sql
   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. At cutover, stop writes to Neon, wait for zero lag, then **copy sequence values**, which logical replication does not carry:

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

6. **Remove the subscription** on KeelDB, which also drops the slot on Neon while Neon is reachable, then drop the publication on Neon:

   ```sql
   -- On KeelDB
   DROP SUBSCRIPTION keeldb_migration;
   -- On Neon
   DROP PUBLICATION keeldb_migration;
   ```

## Verify

Compare row counts table by table:

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

Also check that every extension you need exists on KeelDB, that sequences continue after Neon's highest values, and that your test suite passes. A [branch](/docs/branching) of the migrated database is a safe place to run it.

## Cut over

1. Point app traffic at KeelDB's **pooled** host, `<id>-pooler.us-east.db.keeldb.com`. If your app used Neon's `-pooler` host, the same transaction mode rules apply: `SET LOCAL` inside transactions, transaction-level advisory locks, and the direct host for `LISTEN`. See [Connecting](/docs/connect#transaction-mode-caveats).
2. Point migrations (Prisma Migrate, drizzle-kit and similar) at the **direct** host, for example as `DIRECT_URL`. See [Prisma](/docs/prisma) and [Drizzle](/docs/drizzle).
3. Keep `connect_timeout=10` in the connection string: each KeelDB hostname can return several proxy addresses, and the timeout lets the client move on quickly when one is unreachable.
4. Make sure the database's [IP allowlist](/docs/security#ip-allowlist) includes your app's outbound addresses, or is empty.
5. Take an on-demand backup on KeelDB (**Back up now**).
6. Keep Neon read-only until you are confident, then remove the publication, any leftover slot and the old credentials.

## Recreate your branches

Neon branches don't move with a dump. On KeelDB, create branches of the migrated database from the **Branches** tab, with [`POST /api/v1/databases/{id}/branches`](/docs/api#create-a-branch) or with the `create_branch` [MCP tool](/docs/mcp). On Free and Launch they are copy-on-write, so they are ready without copying the data. Give temporary ones an [expiry time](/docs/branching#branch-expiry).

To weigh the two services first, see [KeelDB and Neon compared](/compare/neon).
