# Seeding (/docs/orm/v6/prisma-migrate/workflows/seeding)

Location: ORM > v6 > Prisma Migrate > Workflows > Seeding

This guide describes how to seed your database using Prisma Client and Prisma ORM'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, such as a default language or 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 ORM [#how-to-seed-your-database-in-prisma-orm]

Prisma ORM's integrated seeding functionality expects a command in the `"seed"` key in the `migrations` object of your `prisma.config.ts`. 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.

```ts
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
    seed: "tsx prisma/seed.ts",
  },
  datasource: {
    url: env("DATABASE_URL"),
  },
});
```

Integrated seeding with Prisma Migrate [#integrated-seeding-with-prisma-migrate]

Database seeding happens when you run `prisma db seed`. 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.

> [!NOTE]
> Prisma ORM v7 changes
> 
> In Prisma ORM v7, seeding is **only triggered explicitly** by running `npx prisma db seed`. Automatic seeding during `prisma migrate dev` or `prisma migrate reset` has been removed.

<details>
  <summary>
    Prisma ORM v6 and earlier
  </summary>

  In Prisma ORM v6 and earlier, Prisma Migrate also integrates seamlessly with your seeds. Seeding is triggered automatically when Prisma Migrate resets the development database.

  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.
  * The database is actually created by `prisma migrate dev`, because it did not exist before.

  When you want to use `prisma migrate dev` or `prisma migrate reset` without seeding, you can pass the `--skip-seed` flag.
</details>

Example seed scripts [#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 [#seeding-your-database-with-typescript-or-javascript]

#### TypeScript

1. Create a new file named `seed.ts`. This can be placed anywhere within your project's folder structure. The example below places it in the `/prisma` folder.

    2. In the `seed.ts` file, import Prisma Client, initialize it and create some records. As an example, take the following Prisma schema with a `User` and `Post` model:

       ```prisma title="schema.prisma" showLineNumbers
       model User {
         id    Int    @id @default(autoincrement())
         email String @unique
         name  String
         posts Post[]
       }

       model Post {
         id        Int     @id @default(autoincrement())
         title     String
         content   String
         published Boolean
         user      User    @relation(fields: [userId], references: [id])
         userId    Int
       }
       ```

       Create some new users and posts in your `seed.ts` file:

       ```js title="seed.ts"
       import { PrismaClient } from "../prisma/generated/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()
         .then(async () => {
           await prisma.$disconnect();
         })
         .catch(async (e) => {
           console.error(e);
           await prisma.$disconnect();
           process.exit(1);
         });
       ```

    3. Add `typescript`, `tsx` and `@types/node` development dependencies:

       
         

#### npm

```bash
npm install -D typescript tsx @types/node
```

#### pnpm

```bash
pnpm add -D typescript tsx @types/node
```

#### yarn

```bash
yarn add --dev typescript tsx @types/node
```

#### bun

```bash
bun add --dev typescript tsx @types/node
```
       

    4. Add the `seed` field to your `prisma.config.ts` file:
       ```ts title="prisma.config.ts"
       import "dotenv/config";
       import { defineConfig, env } from "prisma/config";
       export default defineConfig({
         schema: "prisma/schema.prisma",
         migrations: {
           path: "prisma/migrations",
           seed: "tsx prisma/seed.ts", // [!code ++]
         },
         datasource: {
           url: env("DATABASE_URL"),
         },
       });
       ```

    5. To seed the database, run the `db seed` CLI command:
       
         

#### npm

```bash
npx prisma db seed
```

#### pnpm

```bash
pnpm dlx prisma db seed
```

#### yarn

```bash
yarn dlx prisma db seed
```

#### bun

```bash
bunx --bun prisma db seed
```

#### JavaScript

1. Create a new file named `seed.js`. This can be placed anywhere within your project's folder structure. The below example places it in the `/prisma` folder.

    2. In the `seed.js` file, import Prisma Client, initialize it and create some records. As an example, take the following Prisma schema with a `User` and `Post` model:

       ```prisma title="schema.prisma" showLineNumbers
       generator client {
         provider = "prisma-client"
         output   = "./generated"
       }

       datasource db {
         provider = "postgresql"
       }

       model User {
         id    Int    @id @default(autoincrement())
         email String @unique
         name  String
         posts Post[]
       }

       model Post {
         id        Int     @id @default(autoincrement())
         title     String
         content   String
         published Boolean
         user      User    @relation(fields: [userId], references: [id])
         userId    Int
       }
       ```

       Create some new users and posts in your `seed.js` file:

       ```js title="seed.js"
       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()
         .then(async () => {
           await prisma.$disconnect();
         })
         .catch(async (e) => {
           console.error(e);
           await prisma.$disconnect();
           process.exit(1);
         });
       ```

    3. Add the `seed` field to your `prisma.config.ts` file:

       ```ts title="prisma.config.ts"
       import "dotenv/config";
       import { defineConfig, env } from "prisma/config";
       export default defineConfig({
         schema: "prisma/schema.prisma",
         migrations: {
           path: "prisma/migrations",
           seed: "node prisma/seed.js", // [!code ++]
         },
         datasource: {
           url: env("DATABASE_URL"),
         },
       });
       ```

    4. To seed the database, run the `db seed` CLI command:
       
         

#### npm

```bash
npx prisma db seed
```

#### pnpm

```bash
pnpm dlx prisma db seed
```

#### yarn

```bash
yarn dlx prisma db seed
```

#### bun

```bash
bunx --bun prisma db seed
```

Seeding your database via raw SQL queries [#seeding-your-database-via-raw-sql-queries]

You can also make use of raw SQL queries in order to seed the database with data.

While you can use a plain-text `.sql` file (such as a data dump) for that, it is often
easier to place those raw queries, if they're of short size, into the `seed.js` file
because it saves you the hassle of working out database connection strings and creating
a dependency on a binary like `psql`.

To seed additional data to the `schema.prisma` above, add the following to the
`seed.js` (or `seed.ts`) file:

```js title="seed.js"
async function rawSql() {
  const result =
    await prisma.$executeRaw`INSERT INTO "User" ("id", "email", "name") VALUES (3, 'foo@example.com', 'Foo') ON CONFLICT DO NOTHING;`;
  console.log({ result });
}
```

and chain this function to the promise calls, such as the following change towards
the end of the file:

```js title="seed.js"
main()
  .then(rawSql)
  .then(async () => {
    await prisma.$disconnect();
  })
  .catch(async (e) => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  });
```

Seeding your database via any language (with a Bash script) [#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`:

```bash title="seed.sh"
#!/bin/sh
# -e Exit immediately when a command returns a non-zero status.
# -x Print commands before they are executed
set -ex
# Seeding command go
go run ./seed/
```

The following example uses [psql](https://www.postgresql.org/docs/13/app-psql.html) to run a SQL script in the same folder as `seed.sh`:

```bash title="seed.sh"
#!/bin/sh
# -e Exit immediately when a command returns a non-zero status.
# -x Print commands before they are executed
set -ex
# Seeding command
psql file.sql
```

User-defined arguments [#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:

```js title="seed.js"
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](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02) — `--` —, followed by your custom arguments:

  

#### npm

```bash
npx prisma db seed -- --environment development
```

#### pnpm

```bash
pnpm dlx prisma db seed -- --environment development
```

#### yarn

```bash
yarn dlx prisma db seed -- --environment development
```

#### bun

```bash
bunx --bun prisma db seed -- --environment development
```

Going further [#going-further]

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

* [Supabase community project](https://github.com/supabase-community/seed)
* [Replibyte](https://www.replibyte.com/docs/introduction/)

## Related pages

- [`Baselining a database`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/baselining): How to initialize a migration history for an existing database that contains important data.
- [`Customizing migrations`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/customizing-migrations): How to edit a migration file before applying it to avoid data loss in production.
- [`Data migrations`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/data-migration): How to migrate data using Prisma ORM with the expand and contract pattern.
- [`Development and production`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/development-and-production): Development and production
- [`Generating down migrations`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/generating-down-migrations): How to generate down migrations