How to use the shadow database in Prisma
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
The “shadow database” concept in Prisma is primarily used during migrations to ensure that your database schema changes are safe. It acts as a temporary database where Prisma tests out the migrations before they are applied to the main database.
This allows for a safer migration process because any potential issues are caught on the shadow database, rather than the main one.
We’ve put together this tutorial to show you how it works.
Prisma is an open-source database toolkit that aims to make working with databases easier and more productive for developers. At its core, Prisma comprises:
Now that we’ve introduced Prisma, let’s dive into one of its features: the shadow database.
Before diving into how to use it, you should know that Prisma uses the shadow database automatically during the migration process. Prisma connects to this database, runs the migrations, and then throws it away. If the migrations are successful on the shadow database, they are likely to also be successful on your main database.
To enable the use of a shadow database, your database user must have permissions to:
In many production environments, your main database user might not have these permissions for security reasons. If that’s the case, Prisma allows you to define a special database user just for the migrations.
Modify your schema.prisma file by adding a migrations field in the datasource block:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
migrations = env("DATABASE_MIGRATIONS_URL")
}
Now, in your .env file (or your environment configuration), set the DATABASE_MIGRATIONS_URL to the connection string of your migration-specific user.
DATABASE_URL=postgresql://main_user:password@localhost:5432/mydb
DATABASE_MIGRATIONS_URL=postgresql://migrations_user:password@localhost:5432/mydb
When you’re ready to create a migration, use the following command:
prisma migrate dev --name your_migration_name
This will:
prisma/migrations directoryYou can check the status of your migrations using:
prisma migrate status
In a production setting, you typically use:
prisma migrate deploy
This command also uses the shadow database (if permissions allow) to test out migrations before they’re applied to the main database.
If a migration fails on the shadow database, Prisma will provide error messages indicating what went wrong. This helps you catch potential issues early on, and you can then make the necessary adjustments to your migration files or schema before trying again.
The shadow database feature in Prisma provides an extra layer of safety during database migrations. It ensures that your migrations are tested on a temporary database before being applied to the main one. By combining this with best practices like backups and thorough testing, you can reduce the risks associated with database migrations.
Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.