0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · Lesson

Database Seeding & Connection Pooling

Populate your database with reliable seed data and configure Prisma connection pooling so your Next.js app stays fast and stable under load.

Database Seeding & Connection Pooling is a free Next.js 15 Fullstack (App Router + Server Actions) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Next.js 15 Fullstack (App Router + Server Actions) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Seed a Database?

Seeding fills your database with predictable starter data: admin users, demo records, lookup tables. It lets every developer and every CI run start from the same known state.

Without seeds, manual data entry makes tests flaky and onboarding slow.

The Prisma Seed Script

Prisma runs a seed file you point it at. Create prisma/seed.ts and register it in package.json under the prisma.seed key so prisma db seed knows what to execute.

{
  "prisma": {
    "seed": "ts-node prisma/seed.ts"
  }
}

Writing Seed Logic

Inside the seed file you instantiate the client and create records. Wrap it in an async function and disconnect when finished.

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

async function main() {
  await prisma.user.create({
    data: { email: 'admin@app.com', name: 'Admin' }
  });
}

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

Idempotent Seeds with upsert

Running a seed twice should not create duplicates. Use upsert so a record is created if missing and updated if it already exists.

await prisma.user.upsert({
  where: { email: 'admin@app.com' },
  update: {},
  create: { email: 'admin@app.com', name: 'Admin' }
});

Bulk Seeding

To insert many rows efficiently, use createMany. The skipDuplicates option avoids errors on unique conflicts.

await prisma.tag.createMany({
  data: [
    { name: 'news' },
    { name: 'sports' },
    { name: 'tech' }
  ],
  skipDuplicates: true
});

Running the Seed

Trigger the script from the CLI. Prisma also runs it automatically after prisma migrate reset, giving you a clean reseeded database.

npx prisma db seed

The Connection Problem in Serverless

Next.js on Vercel runs serverless functions. Each invocation can open a fresh DB connection, and under load you quickly exhaust the database connection limit.

Connection pooling solves this by reusing a fixed set of connections.

Reusing the Prisma Client

In development, hot reload can spawn many clients. Store a single instance on globalThis so only one client exists per process.

import { PrismaClient } from '@prisma/client';
const g = globalThis as unknown as { prisma?: PrismaClient };
export const prisma = g.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') g.prisma = prisma;

Pooling with a Connection URL

Use a pooler such as PgBouncer or Prisma Accelerate. Point your DATABASE_URL at the pooled endpoint and add ?pgbouncer=true so Prisma disables prepared statements that PgBouncer cannot share.

DATABASE_URL="postgresql://user:pass@host:6543/db?pgbouncer=true&connection_limit=1"

Direct URL for Migrations

Pooled connections cannot run migrations. Add a directUrl in your schema that bypasses the pooler so prisma migrate works.

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}

Best Practices

Keep your data layer healthy:

  • Make seeds idempotent with upsert
  • Reuse a single client instance
  • Use a pooler in serverless environments
  • Keep a separate direct URL for migrations

Quick Check

Test what you learned about pooling.

Recap

You learned to seed and pool your database:

  • Configure prisma.seed and write idempotent seeds with upsert
  • Bulk insert with createMany
  • Reuse a single Prisma client to avoid leaks
  • Use a pooled DATABASE_URL plus a directUrl for migrations

Now your database starts in a known state and scales safely.

Frequently asked questions

Is the “Database Seeding & Connection Pooling” lesson free?

Yes — the full text of “Database Seeding & Connection Pooling” is free to read here on the web, and the Next.js 15 Fullstack (App Router + Server Actions) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Next.js 15 Fullstack (App Router + Server Actions) course, upgrade to CoddyKit PRO.

What will I learn in “Database Seeding & Connection Pooling”?

Populate your database with reliable seed data and configure Prisma connection pooling so your Next.js app stays fast and stable under load. You practise Next.js 15 Fullstack (App Router + Server Actions) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Next.js 15 Fullstack (App Router + Server Actions)?

No prior experience is required. Next.js 15 Fullstack (App Router + Server Actions) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Database Seeding & Connection Pooling” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Next.js 15 Fullstack (App Router + Server Actions) lesson?

Yes. Every Next.js 15 Fullstack (App Router + Server Actions) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Setting Up Prisma ORM
  2. CRUD with Server Actions
  3. Database Schema Migrations
  4. Database Seeding & Connection Pooling
← Back to Next.js 15 Fullstack (App Router + Server Actions)