Prisma P3009: migrate found failed migrations in the target database
An earlier prisma migrate deploy started a migration and never finished it, and Prisma recorded that in the _prisma_migrations table. Until that record is resolved, no new migration is applied. Find the migration and why it failed, fix the SQL or the database, mark it with prisma migrate resolve --rolled-back or --applied, and run migrate deploy again, before the app starts rather than in the build.
By Livops team, the people who build and run LivopsUpdated
The message, as Prisma’s source defines it[3], with your migration’s name and time filled in:
P3009
migrate found failed migrations in the target database, new migrations will not be applied. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
The `20261201091400_add_user_email` migration started at <time> failed
You see it from prisma migrate deploy, usually on the deploy after the one where a migration broke. This page explains what Prisma recorded, how to find the migration and the reason, the two ways out with prisma migrate resolve, and why migrate deploy belongs before the start and not in the build. It ends with how migrations run on Livops.
What does P3009 mean?
Prisma keeps every migration it has run in the _prisma_migrations table. migrate deploy writes a row when a migration starts and marks it finished when the SQL succeeds; when the SQL fails, it stores the database’s error in that row’s logs[4]. Before applying anything, migrate deploy looks for rows that have no finished_at and no rolled_back_at. If it finds one, it stops with P3009 and applies nothing[4][1][2].
So P3009 is not the original failure. The run where the migration broke usually printed P3018, which carries the real database error[3]:
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 20261201091400_add_user_email
Database error code: ...
Database error:
...
When the failed row has logs, P3009 repeats them after failed with the following logs:[4]. That is where to look first.
| Code | Message (shortened) | When you see it |
|---|---|---|
P3018 |
A migration failed to apply | The run where the SQL failed[3] |
P3009 |
migrate found failed migrations in the target database | Every migrate deploy after that[3] |
P3008 |
The migration … is already recorded as applied in the database | resolve --applied on a migration that is already applied[3] |
P3011 |
Migration … cannot be rolled back because it was never applied to the database | resolve --rolled-back with a wrong or shortened name[3] |
P3012 |
Migration … cannot be rolled back because it is not in a failed state | resolve --rolled-back on a migration that did not fail[3] |
Why a migration fails
Prisma’s docs list the usual reasons[5]:
- The SQL has an error, often after the migration file was edited by hand before it ran.
- A required (
NOT NULL) column was added to a table that already has rows. It works on an empty development database and fails on production data. - The migration process stopped unexpectedly, for example a build or a start that was cancelled or killed halfway.
- The database shut down during the migration.
After a deploy, add one more: the object already exists. A table or column created earlier by prisma db push or by hand makes the migration that creates it fail on production.
How to fix P3009
1. Find the migration and the reason
The P3009 message names the migration. prisma migrate status compares the migrations folder with _prisma_migrations and exits with code 1 when failed migrations are found[6]:
npx prisma migrate status
Each row in _prisma_migrations keeps the error in its logs column[5]. To see the failed rows directly:
SELECT migration_name, started_at, logs
FROM _prisma_migrations
WHERE finished_at IS NULL AND rolled_back_at IS NULL;
2. Check what the failed migration left behind
A migration can be partly executed: in Prisma’s own example, the first two steps succeed and the third fails[5]. Compare the migration’s migration.sql with the database. Which of its tables, columns and indexes already exist? That decides the next step. Take a backup before you change anything.
3a. Roll back, fix, deploy again
Use this when the migration should run again, usually because its SQL needs a fix. Prisma’s steps[5]:
- Remove what the failed attempt created, or make the migration safe to run twice, for example
CREATE TABLE ... IF NOT EXISTS. - Fix the cause in
migration.sql. For theNOT NULLcase, add the column with a default, or add it nullable, fill it, then make it required. - Mark the migration as rolled back, and deploy:
npx prisma migrate resolve --rolled-back "20261201091400_add_user_email"
npx prisma migrate deploy
--rolled-back only updates the record in _prisma_migrations[5]. migrate deploy then treats a migration whose only record is rolled back as not applied, and runs it again[4]. Use the full folder name: a shortened name gives P3011[3].
3b. Finish it by hand, mark it applied
Use this when the database should end up exactly as the migration describes and it is easier to finish the remaining steps yourself. Run them on the production database, then record the migration without running its SQL[5]:
npx prisma migrate resolve --applied "20261201091400_add_user_email"
migrate resolve works only on a failed migration; on a successful one it gives an error[6]. You must pass either --rolled-back or --applied[6].
Undo or finish with migrate diff
On Prisma 7 you can also let Prisma write the SQL. To undo a partly applied migration, diff the database against the migrations folder, run the result, and mark it rolled back[5]:
npx prisma migrate diff \
--from-config-datasource \
--to-migrations ./prisma/migrations \
--config prisma.config.prod.ts \
--script > backward.sql
npx prisma db execute --config prisma.config.prod.ts --file backward.sql
npx prisma migrate resolve --rolled-back "20261201091400_add_user_email"
To finish it instead, diff the database against the schema with --to-schema, run that script, and resolve --applied[5]. db execute runs the script without touching the migrations table[5]. In Prisma 7 db execute no longer takes --url, so the production database is named in a separate config file, prisma.config.prod.ts in these commands[5].
Run migrate deploy before the start, not in the build
migrate deploy applies pending migrations and is meant for production and test environments[6][7]. It does not reset the database or detect drift[7]. Prisma says it should generally run as part of an automated CI/CD pipeline, and does not recommend running it locally against production[7][8]. Where it runs in that pipeline matters for P3009:
- Not in the build. A build often cannot reach the database, and a build that is cancelled or times out mid-migration is the “process stopped unexpectedly” case above.
prisma generateneeds no database and can stay in the build. - Not inside the start command if the start can be killed.
prisma migrate deploy && node server.jscounts the migration against the platform’s start-up check. A slow migration can make the start look failed, and a process stopped halfway leaves the record P3009 complains about. - Once, before the new version starts. A release or pre-deploy step runs once per deploy, before any instance starts. Several instances running
migrate deployat once are serialized by an advisory lock on PostgreSQL, MySQL and SQL Server, and that lock times out after 10 seconds, which cannot be configured[7]. - Keep the
prismapackage installed where it runs. Platforms that prune dev dependencies remove the CLI; Prisma suggests movingprismatodependenciesthen[8].
Test the migration on a copy of production-like data first. A NOT NULL column that fails on production passes on an empty staging database.
On Livops
- Pre-deploy command. In the service’s settings, the Pre-deploy command runs once before each new version starts, such as migrations:
npx prisma migrate deploy. Static sites do not have one. - A failed migration keeps the old version. If the pre-deploy command ends with an error, the deploy stops as
predeploy_failed: the new version is not started, the old one keeps serving, and the command’s output is in the log. P3018 the first time, P3009 after that. - The build has no database. Keep
prisma migrate deployout of the build command.prisma generateis fine there: when the project hasprismaandprisma/schema.prisma, Livops runsprisma generatebefore the build itself. - The start command has a minute. A start that does not answer within a minute fails as
start_failed, another reason to keep migrations out of it. - Back up before you resolve. On the database page you can take a backup of the current state before risky work. A restore never overwrites a database: it opens as a new database next to the original.
- Running migrate resolve. The database has no public port. From your computer, open it with Connect from your computer, run the SSH tunnel command it shows, and point
DATABASE_URLatlocalhostand the tunnel’s port while you runmigrate statusandmigrate resolve. Or put the resolve in front of the migration for one deploy —npx prisma migrate resolve --rolled-back "<name>" && npx prisma migrate deploy— and set the pre-deploy command back afterwards, since on the next deploy the migration is no longer failed andresolvestops with P3012. - Staging first. A staging environment gets its own empty database of each type production has, so a migration can fail there before it reaches production; it cannot catch a failure that only production’s data causes.
A web app costs 39 credits a month. The pre-deploy failure is described in The app did not start, the database variables and backups in Databases, and a database the migration cannot reach in Prisma P1001: Can’t reach database server.
Questions
What does P3009 mean in Prisma?
That the _prisma_migrations table has a migration that started and never finished, and was not marked as rolled back. Prisma refuses to apply new migrations until you resolve it. The migration failed on an earlier run; P3009 is what every run after that one prints.
Should I use migrate resolve --rolled-back or --applied?
--rolled-back when the migration's changes are not in the database, or you have removed them, and you want migrate deploy to run it again, usually after fixing its SQL. --applied when you have finished its steps by hand and the database already matches the migration: it records the migration as applied without running its SQL.
Does migrate resolve change my tables?
No. It only updates the migration history in _prisma_migrations. Anything the failed migration created stays until you remove it or finish it yourself, which is why you check the database before choosing --rolled-back or --applied.
Can I just delete the row from _prisma_migrations?
Prisma's documented way is migrate resolve, which marks the record rather than removing it. Deleting history rows by hand is outside the documented workflow, and it does nothing about the half-applied changes either.
Why does it keep failing after I marked it rolled back?
Because migrate deploy runs the same migration again, and its SQL fails the same way. Fix the SQL first, or remove what the earlier attempt left behind, for example a table that was already created, then resolve and deploy.
Should prisma migrate deploy run in the build?
No. The build often cannot reach the database, and a build that is stopped halfway can leave exactly this failed record. Run migrate deploy once, as a step before the new version starts, against the production database.
Does P3009 happen with MongoDB?
No. Prisma Migrate, migrate deploy and migrate resolve included, does not support MongoDB; db push is used there instead.
Sources
- Prisma ORM v7 — Error reference (prisma.io). Checked .
- Prisma ORM v6 — Error message reference (prisma.io). Checked .
- prisma-engines source — user-facing-errors, schema_engine.rs (github.com). Checked .
- prisma-engines source — apply_migrations.rs (github.com). Checked .
- Prisma ORM v7 — Patching & hotfixing (prisma.io). Checked .
- Prisma ORM v7 — Prisma CLI reference (prisma.io). Checked .
- Prisma ORM v7 — Development and production (prisma.io). Checked .
- Prisma ORM v7 — Deploying database changes with Prisma Migrate (prisma.io). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy with a databaseRelated
- Prisma P1001: Can't reach database server at … — how to fix itPrisma tried the host and port in your connection URL and nothing answered there. After a deploy that is almost always a URL that says localhost, a database that is not attached to the project, or prisma migrate deploy running in a build that has no database. Read the host in the message first: it tells you which.
- ECONNREFUSED / Connection refused: why the app cannot reach its database after deployNothing was listening at the address your app tried. When the address is 127.0.0.1 or localhost, the app never read the database's real address: DATABASE_URL is not set, is not read, or is read during a build that has no database. Attach the database, read DATABASE_URL (or REDIS_URL, MYSQL_URL) at runtime, run migrations before the start rather than in the build, and keep the connection pool under the server's limit.
- error: Environment variable not found: DATABASE_URL (Prisma P1012): how to fix iterror: Environment variable not found: DATABASE_URL is Prisma's P1012 schema validation error: schema.prisma reads the connection string with env("DATABASE_URL") and the process has no such variable. After a deploy it is a .env file that never reached the server, a variable under another name, a migration run in the build, or a prisma.config.ts that stops Prisma loading .env. Set DATABASE_URL in the server's environment and run migrations where it exists.
- Prisma Client could not locate the Query Engine for runtime: how to fix itPrisma Client could not locate the Query Engine for runtime debian-openssl-3.0.x, or the older Query engine library for current platform could not be found, means the client was generated for another platform, usually your laptop, or its engine file was not copied. Run prisma generate where the app is built, add the runtime to binaryTargets, and do not ship node_modules. Prisma 7 has no Rust engine, so the error ends there.
- Managed PostgreSQL hosting in 2026: the options comparedA production PostgreSQL needs backups you can restore, a connection limit your app stays under, the app in the same region, and a supported version. On Livops a PostgreSQL 15, 16 or 17 database runs inside your project, its DATABASE_URL is written into your services, and it is backed up every night; 500 MB is free, one per team, and 1 GB is 9 credits a month. Here is how the paid plans of Neon, Supabase, Render, Railway and a VPS compare; free tiers have their own page.
- Node.js hosting in 2026: how to deploy an Express appA Node.js server in production needs one long-running process that listens on the port it is given, restarts when it crashes and exits cleanly on SIGTERM. On Livops you push a repository with package.json and it runs as a web app for 39 credits a month (39,000 so'm). Here is what Node needs, and how a VPS, Render, Railway, Fly.io, DigitalOcean and Heroku compare.
- A staging environment next to productionOn Livops, Add environment on the project page makes a staging copy of your production services, each with its own address, its own variables and its own empty database, so a test never touches production data. Promote to production then ships the exact release you tested, without rebuilding. A staging environment costs half; pull request previews are billed like regular services.