Seeding your database
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.
"prisma": {"seed": "ts-node prisma/seed.ts"},
ts-node
does transpiling and typechecking by default; typechecking can be disabled with the following flag --transpile-only
."seed": "ts-node --transpile-only prisma/seed.ts"
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
orprisma 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
Create a new file named
seed.ts
. This can be placed anywhere within your projects folder structure. The below example places it in the/prisma
folder.In the
seed.ts
file, import Prisma Client, initialize it and create some records. As an example, take the following Prisma schema with aUser
andPost
model:schema.prisma1model User {2 id Int @id @default(autoincrement())3 email String @unique4 name String5 posts Post[]6}78model Post {9 id Int @id @default(autoincrement())10 title String11 content String12 published Boolean13 user User @relation(fields: [userId], references: [id])14 userId Int15}Create some new users and posts in your
seed.ts
file:seed.ts1import { PrismaClient } from '@prisma/client'2const prisma = new PrismaClient()3async function main() {4 const alice = await prisma.user.upsert({5 where: { email: 'alice@prisma.io' },6 update: {},7 create: {8 email: 'alice@prisma.io',9 name: 'Alice',10 posts: {11 create: {12 title: 'Check out Prisma with Next.js',13 content: 'https://www.prisma.io/nextjs',14 published: true,15 },16 },17 },18 })19 const bob = await prisma.user.upsert({20 where: { email: 'bob@prisma.io' },21 update: {},22 create: {23 email: 'bob@prisma.io',24 name: 'Bob',25 posts: {26 create: [27 {28 title: 'Follow Prisma on Twitter',29 content: 'https://twitter.com/prisma',30 published: true,31 },32 {33 title: 'Follow Nexus on Twitter',34 content: 'https://twitter.com/nexusgql',35 published: true,36 },37 ],38 },39 },40 })41 console.log({ alice, bob })42}43main()44 .then(async () => {45 await prisma.$disconnect()46 })47 .catch(async (e) => {48 console.error(e)49 await prisma.$disconnect()50 process.exit(1)51 })Add
typescript
,ts-node
and@types/node
development dependencies:npm install -D typescript ts-node @types/node
Add the
prisma.seed
field to yourpackage.json
file:package.json1{2 "name": "my-project",3 "version": "1.0.0",4 "prisma": {5 "seed": "ts-node prisma/seed.ts"6 },7 "devDependencies": {8 "@types/node": "^14.14.21",9 "ts-node": "^9.1.1",10 "typescript": "^4.1.3"11 }12}Some projects may require you to add compile options. When using Next.js for example, you would setup your seed script like so:
package.json1"prisma": {2 "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"3},To seed the database, run the
db seed
CLI command:npx prisma db seed
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.
The following example runs a Go script in the same folder as seed.sh
:
seed.sh
1#!/bin/sh2# -e Exit immediately when a command returns a non-zero status.3# -x Print commands before they are executed4set -ex5# Seeding command6go run ./seed/
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:
"seed.js"
1import { parseArgs } from 'node:util'23const options = {4 environment: { type: 'string' },5}67async function main() {8 const {9 values: { environment },10 } = parseArgs({ options })1112 switch (environment) {13 case 'development':14 /** data for your development */15 break16 case 'test':17 /** data for your test environment */18 break19 default:20 break21 }22}2324main()25 .then(async () => {26 await prisma.$disconnect()27 })28 .catch(async (e) => {29 console.error(e)30 await prisma.$disconnect()31 process.exit(1)32 })
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: