apps/docs/content/docs/orm/prisma-migrate/understanding-prisma-migrate/shadow-database.mdx
The shadow database is a second, temporary database that is created and deleted automatically* each time you run prisma migrate dev and is primarily used to detect problems such as schema drift or potential data loss of the generated migration.
migrate diff command also requires a shadow database when diffing against a local migrations directory with --from-migrations or --to-migrations.
:::warning
The shadow database is not required in production, and is not used by production-focused commands such as prisma migrate resolve and prisma migrate deploy.
:::
When you run prisma migrate dev to create a new migration, Prisma Migrate uses the shadow database to:
To detect drift in development, Prisma Migrate:
shadowDatabaseUrl)If Prisma Migrate does not detect schema drift, it moves on to generating new migrations.
:::info[Note]
The shadow database is not responsible for checking if a migration file has been edited or deleted. This is done using the checksum field in the _prisma_migrations table.
:::
If Prisma Migrate detects schema drift, it outputs detailed information about which parts of the database have drifted. The following example output could be shown when the development database has been modified manually: The Color enum is missing the expected variant RED and includes the unexpected variant TRANSPARENT:
[*] Changed the `Color` enum
[+] Added variant `TRANSPARENT`
[-] Removed variant `RED`
Assuming Prisma Migrate did not detect schema drift, it moves on to generating new migrations from Prisma schema changes. To generate new migrations, Prisma Migrate:
--create-only flag)shadowDatabaseUrl are not dropped, but are reset at the start of the migrate dev command)In some cases it might make sense (e.g. when creating and dropping databases is not allowed on cloud-hosted databases) to manually define the connection string and name of the database that should be used as the shadow database for migrate dev. In such a case you can:
SHADOW_DATABASE_URL (or .env file)shadowDatabaseUrl field in prisma.config.ts under the datasource object. In Prisma 6 and below, add the shadowDatabaseUrl field to the datasource block in your schema.prisma file.import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
shadowDatabaseUrl: env("SHADOW_DATABASE_URL"), // [!code highlight]
},
});
:::warn[Important]
Do not use the exact same values for url and shadowDatabaseUrl as that might delete all the data in your database.
:::
Some cloud providers do not allow you to drop and create databases with SQL. Some require to create or drop the database via an online interface, and some really limit you to 1 database. If you develop in such a cloud-hosted environment, you must:
SHADOW_DATABASE_URLshadowDatabaseUrl field in prisma.config.ts under the datasource object. In Prisma 6 and below, add the shadowDatabaseUrl field to the datasource block in your schema.prisma file.import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
shadowDatabaseUrl: env("SHADOW_DATABASE_URL"), // [!code highlight]
},
});
:::warn[Important]
Do not use the exact same values for url and shadowDatabaseUrl as that might delete all the data in your database.
:::
In order to create and delete the shadow database when using migrate dev, Prisma Migrate currently requires that the database user defined in your datasource has permission to create databases.
| Database | Database user requirements |
|---|---|
| SQLite | No special requirements. |
| MySQL/MariaDB | Database user must have CREATE, ALTER, DROP, REFERENCES ON *.* privileges |
| PostgreSQL | The user must be a super user or have CREATEDB privilege. See CREATE ROLE (PostgreSQL official documentation) |
| Microsoft SQL Server | The user must be a site admin or have the SERVER securable. See the official documentation. |
Prisma Migrate throws the following error if it cannot create the shadow database with the credentials your connection URL supplied:
Error: A migration failed when applied to the shadow database
Database error: Error querying the database: db error: ERROR: permission denied to create database
To resolve this error:
prisma db push instead of the prisma migrate dev command.:::info[Important]
The shadow database is only required in a development environment (specifically for the prisma migrate dev command) - you do not need to make any changes to your production environment.
:::