Node.js, Prisma & PostgreSQL

Lesson 11 of 12

Seeding and Testing Your Database

A seed script fills a fresh database with starter data, so you (and your teammates) aren't testing against an empty database every time.

// prisma/seed.ts
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

async function main() {
  await prisma.user.create({
    data: {
      email: 'ada@example.com',
      name: 'Ada',
      posts: { create: [{ title: 'My first post' }] },
    },
  });
}

main().finally(() => prisma.$disconnect());

Wire it up in package.json:

{
  "prisma": {
    "seed": "tsx prisma/seed.ts"
  }
}

Then run it with:

npx prisma db seed

Resetting during development

npx prisma migrate reset

Drops your dev database, reapplies every migration from scratch, and reruns your seed script, an easy way to get back to a known, clean state while building.

TIP

This whole platform's own course catalog, including this lesson, is generated by exactly this kind of seed script.

📝 Seeding Quiz

Passing score: 70%
  1. 1.What is the purpose of a seed script?

  2. 2.npx prisma migrate ____ drops the dev database, reapplies migrations, and reruns the seed script.

  3. 3.A seed script is typically run against a production database with real user data.