Seeding your database

This guide describes how to seed your database using Prisma Client and Prisma's integrated seeding functionality (Preview). 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 particulary useful if you are using Prisma Migrate, which sometimes requires resetting your development database.

Watch the following video for an introduction to the db seed command:

Seeding your database with TypeScript

If you are using TypeScript, follow these steps:

  1. Create a new file prisma/seed.ts.

  2. In the prisma/seed.ts file, import Prisma Client, instantiate it, and create some records. For example:

    import { PrismaClient } from '@prisma/client'
    const prisma = new PrismaClient()
    async function main() {
    const alice = await prisma.user.upsert({
    where: { email: 'alice@prisma.io' },
    update: {},
    create: {
    email: `alice@prisma.io`,
    name: 'Alice',
    posts: {
    create: {
    title: 'Check out Prisma with Next.js',
    content: 'https://www.prisma.io/nextjs',
    published: true,
    },
    },
    },
    })
    const bob = await prisma.user.upsert({
    where: { email: 'bob@prisma.io' },
    update: {},
    create: {
    email: `bob@prisma.io`,
    name: 'Bob',
    posts: {
    create: [
    {
    title: 'Follow Prisma on Twitter',
    content: 'https://twitter.com/prisma',
    published: true,
    },
    {
    title: 'Follow Nexus on Twitter',
    content: 'https://twitter.com/nexusgql',
    published: true,
    },
    ],
    },
    },
    })
    console.log({ alice, bob })
    }
    main()
    .catch(e => {
    console.error(e)
    process.exit(1)
    })
    .finally(async () => {
    await prisma.$disconnect()
    })
  3. Add typescript, ts-node and @types/node development dependencies:

    npm install -D typescript ts-node @types/node
  4. Optionally, add additional --compiler-options to the script that Prisma uses to run the seed script (2.18.0 and later). This makes prisma db seed work with tools that have specific requirements when used with TypeScript, such as Next.js. The name of the script is ts-node:

    {
    "name": "my-project",
    "version": "1.0.0",
    "scripts": {
    "ts-node": "ts-node --compiler-options '{\"module\":\"CommonJS\"}'"
    },
    "devDependencies": {
    "@types/node": "^14.14.21",
    "ts-node": "^9.1.1",
    "typescript": "^4.1.3"
    }
    }
  5. To seed the database, run the db seed CLI command:

    prisma db seed --preview-feature

Seeding your database with JavaScript

If you are using JavaScript, follow these steps:

  1. Create a new file prisma/seed.js

  2. In the prisma/seed.js file, import Prisma Client, initialize it and create some records. For example:

    const { PrismaClient } = require('@prisma/client')
    const prisma = new PrismaClient()
    async function main() {
    const alice = await prisma.user.upsert({
    where: { email: 'alice@prisma.io' },
    update: {},
    create: {
    email: `alice@prisma.io`,
    name: 'Alice',
    posts: {
    create: {
    title: 'Check out Prisma with Next.js',
    content: 'https://www.prisma.io/nextjs',
    published: true,
    },
    },
    },
    })
    const bob = await prisma.user.upsert({
    where: { email: 'bob@prisma.io' },
    update: {},
    create: {
    email: `bob@prisma.io`,
    name: 'Bob',
    posts: {
    create: [
    {
    title: 'Follow Prisma on Twitter',
    content: 'https://twitter.com/prisma',
    published: true,
    },
    {
    title: 'Follow Nexus on Twitter',
    content: 'https://twitter.com/nexusgql',
    published: true,
    },
    ],
    },
    },
    })
    console.log({ alice, bob })
    }
    main()
    .catch(e => {
    console.error(e)
    process.exit(1)
    })
    .finally(async () => {
    await prisma.$disconnect()
    })
  3. To seed the database, run the db seed CLI command:

    prisma db seed --preview-feature

Seeding your database with a Bash script

In addition to TypeScript and JavaScript, you can also uses 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/sh
2# -e Exit immediately when a command returns a non-zero status.
3# -x Print commands before they are executed
4set -ex
5# Seeding command
6go run ./seed/

The following example uses psql to run a SQL script in the same folder as seed.sh:

seed.sh
1#!/bin/sh
2# -e Exit immediately when a command returns a non-zero status.
3# -x Print commands before they are executed
4set -ex
5# Seeding command
6psql file.sql

Integrated seeding with Prisma Migrate

Prisma Migrate integrates seamlessly with your seeds, assuming you follow the steps in the previous section. When Prisma Migrate resets the development database, seeding is triggered automatically. 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 drift.
Edit this page on GitHub