Skip to content

Prisma migrations in production

Learn how to apply Prisma migrations in production against a live Postgres or MySQL database without downtime or lost data. You will set up a deploy step that applies migrations safely, catch schema drift early, and ship column changes that survive a rolling deploy.

This guide shows you how to apply Prisma migrations to a live production database, and it is written for the developer who owns the deploy. In development you reach for prisma migrate dev, but that command rewrites your migration history and can drop data, so it does not belong near production. Production uses prisma migrate deploy, which applies the migration files already committed under prisma/migrations/ and refuses to change history. By the end you will have a deploy step that applies migrations before new code takes traffic, and a check for drift, meaning the live database no longer matches your schema file. You will also see the two-step pattern for renaming a column, so old and new versions of your app can run at the same time.

How to verify

Inspect the database against the migration history before you make any change:

npx prisma migrate status                       # which migrations are applied
psql $DATABASE_URL -c "SELECT * FROM _prisma_migrations ORDER BY started_at DESC LIMIT 5;"
npx prisma db pull --print                       # diff schema.prisma vs live schema
git log -- prisma/migrations/ | head -10
ls prisma/migrations/ | tail -5

If prisma migrate status reports “drift detected”, the live schema diverged from schema.prisma outside the migration system. Resolve before any new migration — otherwise migrate deploy may behave unexpectedly.

What’s happening

Prisma tracks applied migrations in a _prisma_migrations table inside the target database. Each migration is a directory under prisma/migrations/<timestamp>_<name>/ containing a single migration.sql (raw SQL that Prisma generated from the schema diff). prisma migrate deploy reads the directory, queries the table to find which are already applied, and runs the pending ones in transactions. It never modifies the schema file and never generates new migrations — those happen in dev.

The split is intentional: development uses the schema as the source of truth and the migrations table reflects what was applied. Production uses the migrations directory as the source of truth and the schema file is just a build artifact. If you edit a migration after it’s been applied in any environment, you create drift; the migration’s checksum is stored in the table and Prisma will refuse to deploy further until you resolve.

The procedure

  1. In CI, before building, deploy pending migrations. The connection string should be a migration-role user with DDL privileges, separate from the runtime user:

    DATABASE_URL=$MIGRATION_URL npx prisma migrate deploy
  2. In production deploy pipelines, run migrate deploy as a step that completes before any new application instance starts taking traffic. The order matters:

    # deploy pipeline excerpt
    - name: Apply migrations
      run: DATABASE_URL=$MIGRATION_URL npx prisma migrate deploy
    - name: Health check
      run: curl --fail https://staging.example.com/health
    - name: Switch upstream to new version
      run: ./bin/flip-blue-green.sh
  3. For rolling deploys (new and old code running simultaneously), every migration must be backward-compatible with the previous app version. Adding a column with a default? Safe. Renaming or dropping? Two-step: add the new column, ship the code that writes both, then a second migration drops the old column.

  4. Baseline existing databases that pre-date Prisma. Generate the initial migration, then mark it as already applied without running:

    npx prisma migrate diff \
      --from-empty \
      --to-schema-datamodel prisma/schema.prisma \
      --script > prisma/migrations/0_init/migration.sql
    npx prisma migrate resolve --applied 0_init
  5. Detect drift in production with a scheduled job. If prisma migrate status reports applied migrations differing from the directory checksums, alert:

    # cron entry on a monitoring host
    */15 * * * * cd /srv/app && npx prisma migrate status > /tmp/migrate-status.txt 2>&1 || \
      ./alert.sh "prisma drift detected"
  6. For rollback, never use migrate reset against production — it drops the database. Instead, write a new down migration manually that reverses the change, and apply it like any other migration:

    mkdir -p prisma/migrations/20260602_rollback_xyz
    echo "ALTER TABLE users DROP COLUMN new_field;" > prisma/migrations/20260602_rollback_xyz/migration.sql
    npx prisma migrate deploy

Common pitfalls

  • prisma migrate deploy does not generate the Prisma Client. Add prisma generate as a separate step before building the app, or rely on the postinstall hook.
  • The DB user for migrations needs DDL (CREATE/ALTER/DROP). The runtime user should not — split them. A leaked runtime credential then cannot reshape the schema.
  • Long-running migrations on Postgres take an ACCESS EXCLUSIVE lock by default. Adding a non-nullable column with a default on a 50 M row table will block reads and writes for minutes. Split: add nullable, backfill in batches, set default, set not-null.
  • The migration’s migration.sql is hand-editable. If you edit one after applying it elsewhere, the checksums diverge. Make a new migration to fix, never edit applied ones.
  • For multi-tenant schemas, Prisma deploys to one schema at a time. Loop over schemas with --schema if your tenant model uses Postgres schemas per tenant.

For the same problem in TypeORM or Sequelize, see TypeORM migrations production and Sequelize migrations production. In the engagements Stack Harbor runs, every Prisma migration is reviewed for backward compatibility before merge; the deploy pipeline runs migrate deploy against a snapshot of production first, and as part of managed operations the migration step is wired into a rollback-safe blue/green flow so a broken migration doesn’t take down the application.