Prisma + PostgreSQL
Type-safe database access with Prisma ORM
Cost: Prisma itself is completely FREE and open-source. However, you still need a PostgreSQL database, which has its own costs. See Supabase (free tier available) orAWS RDS (free tier for 12 months) for database options and pricing.
What is Prisma?
Prisma is a modern database toolkit that replaces traditional ORMs. It provides type-safe database queries, automatic migrations, and a visual data browser. Combined with PostgreSQL, it is a powerful choice for Next.js applications.
Why Prisma?
- Auto-generated TypeScript types from your database schema
- Intuitive query API with autocomplete
- Easy migrations with schema versioning
- Prisma Studio for visual database browsing
Setup Guide
Install Prisma
Add Prisma to your Next.js project as a dev dependency.
Initialize Prisma
Create the Prisma schema file and configure your database connection.
Define Your Schema
Create your data models in schema.prisma using Prisma's intuitive syntax.
Generate Client
Generate the Prisma Client to get type-safe database access.
Run Migrations
Apply your schema changes to the database.
Step 1: Install Prisma
Step 2: Initialize Prisma
✔ Your Prisma schema was created at prisma/schema.prisma ✔ Created .env file
Step 3: Configure Database Connection
# .env
DATABASE_URL="postgresql://username:password@hostname:5432/database?schema=public"Step 4: Define Your Schema
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
email String @unique
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
posts Post[]
}
model Post {
id String @id @default(cuid())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
createdAt DateTime @default(now())
}Step 5: Generate Client and Run Migration
Applying migration '20240115_init' The following migration(s) have been created and applied: migrations/20240115_init/migration.sql ✔ Generated Prisma Client
Prisma Studio
Visual database browser for development:
Prisma Studio is running on http://localhost:5555
Best Practices
- Use
cuid()oruuid()for primary keys (not auto-increment) - Add
createdAtandupdatedAtto all models - Use meaningful migration names (
add_user_rolenotmigration_1) - Review generated SQL before applying migrations
- Keep schema.prisma as the source of truth
- Use transactions for related operations