Prisma
databasesprismaGuideBeginner
Database Seeding with Prisma
How to populate development and staging databases with mock data using prisma/seed.ts and the tsx script runner.
Verified: 2026-10-01
Tested on:prisma 6.x
Platforms:
node
#prisma#seeding#mock-data#development
Database Seeding with Prisma
Database seeding populates initial test data (admin users, categories, sample articles) into a fresh database.
1. Writing the Seed Script (prisma/seed.ts)
// prisma/seed.ts
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
console.log("π± Seeding database...");
// Clean existing tables in development
await prisma.post.deleteMany();
await prisma.user.deleteMany();
// Insert admin user with posts
const admin = await prisma.user.create({
data: {
email: "admin@opendevdocs.com",
name: "Admin User",
role: "ADMIN",
posts: {
create: [
{
title: "Welcome to OpenDevDocs",
content: "The open developer platform for everyone.",
published: true,
},
{
title: "Docker & Kubernetes Guide",
content: "Learn container orchestration from scratch.",
published: true,
},
],
},
},
});
console.log(`β
Database seeded successfully! Admin ID: ${admin.id}`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});2. Configuring package.json
Tell Prisma how to execute your TypeScript seed script:
{
"prisma": {
"seed": "npx tsx prisma/seed.ts"
}
}3. Running the Seed Command
npx prisma db seed[!NOTE] Running
prisma migrate resetalso triggersprisma db seedautomatically after replaying migrations.
Related Topics
Prisma with Node.js & Express
Architecture patterns for using Prisma in Node.js backend services: Express / Fastify REST APIs, error handling middlewares, and clean architecture.
Production Migration Workflow
Deploying database migrations in CI/CD pipelines, Docker containers, Kubernetes jobs, and zero-downtime database release strategies.