
How to run Postgres database migrations without downtime
Most database migration guides assume a team you don't have. Here's the no-downtime approach for Postgres on a single box: three techniques, one real limit.
Running a database migration without downtime on a single Postgres box comes down to one thing: knowing which operations take a heavy ACCESS EXCLUSIVE lock and avoiding holding one longer than your app's request timeout. Most common migrations, adding a column, adding a constraint, adding an index, don't have to hold one for long at all. The exceptions are worth knowing before you run into one live.
Why a naive migration takes your app down
Postgres locks a table for the duration of most ALTER TABLE operations. An ACCESS EXCLUSIVE lock, the level most schema changes require by default, blocks every other read and write against that table until the migration finishes. On a small table that's invisible. On a table with real production rows, a migration that scans the whole table under that lock can take long enough that every request touching it starts timing out, which is what actually causes the outage, not the migration itself.
This is the same real setup Docker + Postgres on a single box already covers: one Postgres instance, no replica to fail over to while a migration runs. The lock-duration problem is the whole game.
First: set a lock timeout
Even a brief ACCESS EXCLUSIVE lock can cause an outage if the ALTER TABLE has to wait for it. While it waits behind a long-running query, every new query on that table queues behind the ALTER. Set a short lock_timeout before each DDL statement, so the migration gives up quickly instead of blocking traffic, and retry it a few times:
SET lock_timeout = '3s';
ALTER TABLE users ADD COLUMN plan_tier text DEFAULT 'free';If it times out, wait a moment and run it again. Most migration tools let you set this per migration, and it applies to every statement in this article.
Three migrations that don't have to cost you anything
| Operation | Lock the naive version takes | No-downtime technique |
|---|---|---|
| Add a column with a default | ACCESS EXCLUSIVE, but brief since Postgres 11 (no table rewrite) | Use a non-volatile DEFAULT directly, no change needed |
Add NOT NULL or a foreign key |
ACCESS EXCLUSIVE for the full table scan | Add NOT VALID first, then VALIDATE CONSTRAINT separately |
| Add an index | SHARE for the full build (blocks writes, not reads) | CREATE INDEX CONCURRENTLY instead of a plain CREATE INDEX |
| Change a column's data type | ACCESS EXCLUSIVE for a full table rewrite | No clean trick, see "Where this breaks down" below |
Adding a column. As of Postgres 11, adding a column with a non-volatile DEFAULT is fast even on a large table, it evaluates the default once and stores it in the table's metadata instead of rewriting every row:
sql
ALTER TABLE users ADD COLUMN plan_tier text DEFAULT 'free';That's still true. The advice to avoid DEFAULT on ADD COLUMN for performance reasons is years out of date for a non-volatile value like a literal or NULL.
Adding a NOT NULL or foreign key constraint. Adding either directly requires Postgres to scan the whole table under the heavy lock to verify every existing row. Split it into two statements instead:
sql
ALTER TABLE users ADD CONSTRAINT plan_tier_not_null
CHECK (plan_tier IS NOT NULL) NOT VALID;
ALTER TABLE users VALIDATE CONSTRAINT plan_tier_not_null;The first statement is nearly instant, because it doesn't check existing rows yet. The second does the scan under a SHARE UPDATE EXCLUSIVE lock, which doesn't block normal reads or writes.
A CHECK constraint isn't the same as a NOT NULL column attribute. To finish the job on Postgres 12 or later, set the attribute and then drop the now-redundant check:
ALTER TABLE users ALTER COLUMN plan_tier SET NOT NULL;
ALTER TABLE users DROP CONSTRAINT plan_tier_not_null;SET NOT NULL still takes an ACCESS EXCLUSIVE lock, but on Postgres 12+ it skips the full table scan when a valid check constraint already proves no row is null, so the lock is brief.
The same two-step pattern works for foreign keys: add the constraint with NOT VALID, then VALIDATE CONSTRAINT separately.
Adding an index. Use CREATE INDEX CONCURRENTLY instead of a plain CREATE INDEX, which blocks writes for the whole build. The concurrent version takes longer, but reads and writes continue while it builds. Two things to know:
CREATE INDEX CONCURRENTLYcan't run inside a transaction block, and many migration tools wrap each migration in a transaction by default. Turn that off for this migration.- If the build fails partway, for example on a duplicate value in a unique index, it leaves an invalid index behind that still slows writes. Check for one with
SELECT indexrelid::regclass FROM pg_index WHERE NOT indisvalid;, drop it withDROP INDEX CONCURRENTLY, fix the cause and retry.
See the CREATE INDEX documentation for details.
Where this breaks down
Changing a column's data type is the one operation without a clean trick here. In most cases it rewrites the entire table and every index on it, under a full lock, for the whole duration. A varchar length increase is a documented exception since Postgres treats it as binary-compatible with no rewrite needed, but most real type changes, an int to a bigint, a text to a jsonb, don't get that exception.
For a single-box setup with no replica, there's no trick that makes this free. The real options are: schedule it for a real maintenance window if the table's small enough to make that tolerable, or handle it the way larger teams do, add a new column of the target type, backfill it in batches, cut over the application, then drop the old column, which trades a fast migration for a multi-step, multi-deploy process. Neither is downtime-free by accident. Pick the one that matches how much the table actually matters.
The three techniques above cover the database migration work a small team runs most often. The type-change case is rare enough, and expensive enough when it hits, that it's worth knowing you'll need a real plan for it before the day you're doing it under pressure.
Update, October 23, 2026: we've added the lock-timeout step, the final SET NOT NULL step and the failure modes of concurrent index builds, and corrected the lock a plain CREATE INDEX takes (SHARE, not ACCESS EXCLUSIVE).