Skip to main content
Prisma Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

db init

db init bootstraps a database to match the current emitted contract and signs it.

It creates everything the contract declares and the database does not have yet, using additive operations only. Structures already in place and compatible are left alone. A conflict that would need a destructive change stops the run.

title="bun"
bunx prisma db init --db "$DATABASE_URL"
pnpm
pnpm prisma db init --db "$DATABASE_URL"
yarn
yarn prisma db init --db "$DATABASE_URL"
npm
npx prisma db init --db "$DATABASE_URL"
Option What it does
--db <url> Connects to the database.
--dry-run Shows planned operations without applying them.
--advance-ref <name> Advances the named ref to the post-command contract hash. Without it, db init advances db when --db is omitted, and advances nothing when --db is passed.
--config <path> Read this config file instead of ./prisma.config.ts.
--json Prints a machine-readable result.

db init is intended for bootstrap work. It creates missing structures needed by the contract and writes the contract marker after the database matches.

Run without --db, db init takes the connection from db.connection in prisma.config.ts and advances the ref named db to the contract it just applied. Pass --db and that advancement is suppressed, even when the URL is the same one the config holds. Pass --advance-ref db alongside --db to get it back.

The db ref is what migration plan uses as its starting point when you do not pass --from. Passing --db only suppresses the advancement; it never removes a db ref that already exists. So a loop that always passes --db without --advance-ref db leaves the ref wherever it was last set, and the next migration plan starts from that stale contract. If the ref was never created, migration plan plans from an empty database while migrations/app/ is empty, and refuses with MIGRATION.PLAN_ORIGIN_UNKNOWN once migrations exist on disk.

Run a dry run first when you are not working with a disposable local database:

title="bun"
bunx prisma db init --db "$DATABASE_URL" --dry-run
pnpm
pnpm prisma db init --db "$DATABASE_URL" --dry-run
yarn
yarn prisma db init --db "$DATABASE_URL" --dry-run
npm
npx prisma db init --db "$DATABASE_URL" --dry-run
title="bun"
bunx prisma contract emit

bunx prisma db init --db "$DATABASE_URL" --advance-ref db

bunx prisma db verify --db "$DATABASE_URL"
pnpm
pnpm prisma contract emit
pnpm prisma db init --db "$DATABASE_URL" --advance-ref db
pnpm prisma db verify --db "$DATABASE_URL"
yarn
yarn prisma contract emit
yarn prisma db init --db "$DATABASE_URL" --advance-ref db
yarn prisma db verify --db "$DATABASE_URL"
npm
npx prisma contract emit
npx prisma db init --db "$DATABASE_URL" --advance-ref db
npx prisma db verify --db "$DATABASE_URL"
title="bun"
bunx prisma db init --db "$DATABASE_URL" --dry-run --json
pnpm
pnpm prisma db init --db "$DATABASE_URL" --dry-run --json
yarn
yarn prisma db init --db "$DATABASE_URL" --dry-run --json
npm
npx prisma db init --db "$DATABASE_URL" --dry-run --json

Use db update when the database already exists and you want Prisma ORM to reconcile it with a changed contract. Use migration plan when you want a reviewable migration package in version control.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu