Skip to main content

Seeding

This guide describes how to seed your database using Prisma Client and Prisma's integrated seeding functionality. Seeding allows you to consistently re-create the same data in your database and can be used to:

  • Populate your database with data that is required for your application to start - for example, a default language or a default currency.
  • Provide basic data for validating and using your application in a development environment. This is particularly useful if you are using Prisma Migrate, which sometimes requires resetting your development database.

How to seed your database in Prisma

Prisma's integrated seeding functionality expects a command in the "seed" key in the "prisma" key of your package.json file. This can be any command, prisma db seed will just execute it. In this guide and as a default, we recommend writing a seed script inside your project's prisma/ folder and starting it with the command.

TypeScript
JavaScript

Integrated seeding with Prisma Migrate

Database seeding happens in two ways with Prisma: manually with prisma db seed and automatically in prisma migrate dev and prisma migrate reset.

With prisma db seed, you decide when to invoke the seed command. It can be useful for a test setup or to prepare a new development environment, for example.

Prisma Migrate also integrates seamlessly with your seeds, assuming you follow the steps in the section below. When Prisma Migrate resets the development database, seeding is triggered automatically if you have a "seed" property in the "prisma" section in your package.json.

Prisma Migrate resets the database and triggers seeding in the following scenarios:

  • You manually run the prisma migrate reset CLI command.
  • The database is reset interactively in the context of using prisma migrate dev - for example, as a result of migration history conflicts or database schema drift.
  • When you want to use prisma migrate dev or prisma migrate reset without seeding, you can pass the --skip-seed flag.

Example seed scripts

Here we suggest some specific seed scripts for different situations. You are free to customize these in any way, but can also use them as presented here:

Seeding your database with TypeScript or JavaScript

TypeScript
JavaScript

Seeding your database via any language (with a Bash script)

In addition to TypeScript and JavaScript, you can also use a Bash script (seed.sh) to seed your database in another language such as Go, or plain SQL.

Go
SQL

User-defined arguments

This feature is available from version 4.15.0 and later.

prisma db seed allows you to define custom arguments in your seed file that you can pass to the prisma db seed command. For example, you could define your own arguments to seed different data for different environments or partially seeding data in some tables.

Here is an example seed file that defines a custom argument to seed different data in different environments:

import { parseArgs } from 'node:util'

const options = {
environment: { type: 'string' },
}

async function main() {
const {
values: { environment },
} = parseArgs({ options })

switch (environment) {
case 'development':
/** data for your development */
break
case 'test':
/** data for your test environment */
break
default:
break
}
}

main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})

You can then provide the environment argument when using prisma db seed by adding a delimiter-- —, followed by your custom arguments:

npx prisma db seed -- --environment development

Going further

Here's a non-exhaustive list of other tools you can integrate with Prisma in your development workflow to seed your database: