Weeks before. Create the schema on the new cluster from a schema-only dump. Then publish on the old server and subscribe on the new one. The subscription copies every table, then streams changes as they happen.
-- old server (wal_level = logical)
CREATE PUBLICATION mig FOR ALL TABLES;
-- new server, after: pg_dump --schema-only shop | psql -h new shop
CREATE SUBSCRIPTION mig
CONNECTION 'host=old.db dbname=shop user=replicator password=...'
PUBLICATION mig;
The gotchas, before the night, not during it. Logical replication does not copy schema changes, so freeze DDL until the switch. It does not copy sequence values, so you set them by hand at cutover. A table with no primary key cannot replicate updates or deletes unless you set REPLICA IDENTITY FULL, which makes every change on it look up the row by every column. Find those tables early.
-- tables with no primary key: fix these before you rely on logical replication
SELECT c.oid::regclass AS table_name
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r' AND n.nspname NOT IN ('pg_catalog', 'information_schema')
AND NOT EXISTS (SELECT 1 FROM pg_constraint p WHERE p.conrelid = c.oid AND p.contype = 'p');
The night. Put the app in maintenance mode so writes stop. Wait until the subscription has applied everything (compare pg_current_wal_lsn() on the old server with latest_end_lsn in pg_stat_subscription on the new one). Copy the sequence values across. Compare row counts on the big tables. Point the app at the new cluster, run the smoke tests, and open writes.
-- on the old server: generate the setval calls for every sequence
SELECT format('SELECT setval(%L, %s, true);', schemaname || '.' || sequencename, last_value)
FROM pg_sequences WHERE last_value IS NOT NULL;
-- run the output on the new serverThe way back. Before you open writes on the new cluster, decide how you would return. The common answer is a reverse subscription, from new to old, created at the switch. Then if something is wrong at 2 AM, the old server already has every order placed since.
Does this work for MySQL to Aurora, or between engines?The shape is the same: full copy, then change data capture, then a short switch. For MySQL into Aurora or RDS, a replica of your server that you promote is the simplest version. Between engines, AWS DMS or Debezium does the full load and the change stream. The rest of this chapter covers those moves and the ways they fail.