How to reset and seed a Prisma database
October 24, 2023
Prisma is an open-source database toolkit that simplifies database workflows, making them more efficient and type-safe.
One of the common tasks during development is to reset your database and seed it with initial data, especially when you're setting up the development environment or testing your application. In this post, we'll walk you through how to do this with Prisma.
Resetting the Prisma database
When you want to revert your database to its initial state or apply new changes to the Prisma schema, you might need to reset it. Here's how:
⚠️ Resetting your database will delete all data and drop the schema in your database.
- Navigate to the root directory of your Prisma project.
- Run the following command to reset the database:
- Drop the database
- Create a new database
- Apply all migrations
- Generate the Prisma client
Seeding the Prisma database
Seeding allows you to populate your database with predefined data. Here's how to set up and run seeding with Prisma:
- Create a seed script
Create a new file named
seed.ts (for TypeScript) or
This file should export a function named
seed that will handle the seeding logic. Here's an example using TypeScript:
- Update Prisma schema
schema.prisma file, you'll need to tell Prisma about your seed script. Modify the
generator block to include a
- Run the seed script
With everything in place, you can now seed your database with the following command:
Prisma will execute the seed script, and your database will be populated with the data you've defined.
Validating the result
You can validate that the database was reset and seeded properly by using a database tool like Basedash. You should validate the schema, check the
_prisma_migrations table to ensure that all migrations succeeded as expected, and view the data in some of the tables that were seeded.
With Prisma's streamlined CLI and the seeding setup, you can make sure your development environment is always in a consistent state. Remember to exercise caution when resetting databases, especially in production environments, to avoid any unintended data loss.
How to automate Prisma migrations in a CI/CD pipeline
How to implement soft deletes in Prisma
How to use the shadow database in Prisma
UUID vs GUID vs CUID vs NanoID: A guide to database primary keys
How to generate UUIDs in Prisma
How to squash migrations in Prisma