If you have ever had a deployment fail, tried to re-run the pipeline, and immediately hit ERROR: relation "users" already exists, your migrations are fragile.
My rule for every project is that I can run the migration script ten times in a row without it failing or corrupting data. Here is what that takes in Postgres.
A migration that assumes an empty database fails on the second run.
ALTER TABLE users ADD COLUMN phone_number VARCHAR(20);
-- Fails if the column exists.
IF NOT EXISTS covers columns, tables and indexes.
ALTER TABLE users ADD COLUMN IF NOT EXISTS phone_number VARCHAR(20);
CREATE INDEX IF NOT EXISTS users_phone_number_idx ON users (phone_number);
It only checks the name. If an index with that name exists on different columns, Postgres skips the statement and gives you a notice.
ADD CONSTRAINT has no IF NOT EXISTS. For those, check the state of the object in a DO block.
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'users_email_unique' AND conrelid = 'users'::regclass
) THEN
ALTER TABLE users ADD CONSTRAINT users_email_unique UNIQUE (email);
END IF;
END $$;
For a unique rule on a column, a unique index does the same job in one line.
CREATE UNIQUE INDEX IF NOT EXISTS users_email_unique_idx ON users (email);
Seeding is where it usually goes wrong. You want a default admin user or a few configuration rows.
INSERT INTO settings (key, value) VALUES ('tax_rate', '0.05');
Run this twice and you have two tax rates, or a primary key collision.
INSERT INTO settings (key, value)
VALUES ('tax_rate', '0.05')
ON CONFLICT (key) DO UPDATE SET value = '0.05';
If the record exists, it is corrected to the desired state. If it doesn’t, it is created. This needs a primary key or unique constraint on key. Without one, Postgres rejects the statement.
Moving data from one column to another is dangerous if the migration isn’t idempotent.
UPDATE orders SET total_cents = total * 100;
Every run rewrites every row. Run it again after the app has started writing total_cents directly, and you overwrite those values with whatever is still in total. Limit the update to rows that haven’t been moved.
UPDATE orders
SET total_cents = total * 100
WHERE total_cents IS NULL;
A runner with a schema_migrations table won’t re-run a migration it has recorded as finished. It can’t help with one that failed partway. If a migration isn’t wrapped in a transaction, its first statements stay applied after the failure, and the retry starts again from the top.
Postgres can run most DDL inside a transaction, which covers much of this. Some statements can’t run in one, including CREATE INDEX CONCURRENTLY, so a migration using them has no rollback to fall back on. A failed concurrent build also leaves an invalid index behind, and IF NOT EXISTS will treat that index as already there. Drop it before you retry.
AI agents, automation, and websites, built in a few weeks and then maintained for a monthly fee. If something in your week should be software by now, book a call.