Prisma + PostgreSQL

Recommended

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

1

Install Prisma

Add Prisma to your Next.js project as a dev dependency.

2

Initialize Prisma

Create the Prisma schema file and configure your database connection.

3

Define Your Schema

Create your data models in schema.prisma using Prisma's intuitive syntax.

4

Generate Client

Generate the Prisma Client to get type-safe database access.

5

Run Migrations

Apply your schema changes to the database.

Step 1: Install Prisma

Terminal
$npm install prisma --save-dev && npm install @prisma/client

Step 2: Initialize Prisma

Terminal
$npx prisma init
✔ Your Prisma schema was created at prisma/schema.prisma
✔ Created .env file

Step 3: Configure Database Connection

Bash
# .env
DATABASE_URL="postgresql://username:password@hostname:5432/database?schema=public"

Step 4: Define Your Schema

PRISMA
// 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

Terminal
$npx prisma migrate dev --name init
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:

Terminal
$npx prisma studio
Prisma Studio is running on http://localhost:5555

Best Practices

  • Use cuid() or uuid() for primary keys (not auto-increment)
  • Add createdAt and updatedAt to all models
  • Use meaningful migration names (add_user_role not migration_1)
  • Review generated SQL before applying migrations
  • Keep schema.prisma as the source of truth
  • Use transactions for related operations

AWS Deployment Guide — Built with Next.js