Prisma + PostgreSQL
Set up Prisma ORM with PostgreSQL for fully type-safe database access, migrations, and seeding in a Node.js or Next.js project.
Initialize the Project
Create a new Node.js project with TypeScript. Skip this step if you're adding Prisma to an existing project.
mkdir my-prisma-app && cd my-prisma-app && npm init -y && npm install -D typescript ts-node @types/node && npx tsc --initInstall Prisma
Install the Prisma CLI as a dev dependency and the Prisma Client as a runtime dependency.
npm install -D prisma && npm install @prisma/clientInitialize Prisma
Run prisma init to create prisma/schema.prisma and a .env file. Pass --datasource-provider postgresql to pre-configure the provider.
npx prisma init --datasource-provider postgresqlConfigure Database URL
Set your PostgreSQL connection string in .env. For cloud providers, grab the connection string from their dashboard.
Define the Schema
Model your data in prisma/schema.prisma. Prisma models map 1:1 to database tables. Relations are declared with @relation.
Run the First Migration
Create and apply a migration. Prisma generates SQL and stores the migration history in prisma/migrations/. Always review the generated SQL before applying in production.
npx prisma migrate dev --name initCreate a Singleton Prisma Client
In development, Next.js hot-reload can create many Prisma Client instances. A singleton pattern prevents connection pool exhaustion.
Perform CRUD Operations
Use the generated Prisma Client to query your database. All methods are fully typed based on your schema.
Seed the Database
Create prisma/seed.ts to populate the database with initial data. Register the seed script in package.json so prisma migrate reset runs it automatically.
Open Prisma Studio
Launch Prisma Studio — a visual browser-based GUI to inspect and edit your database data without writing SQL.
npx prisma studioFound this guide helpful? Check out more tools and guides.
Browse All GuidesPrerequisites
- Node.js 18+
- PostgreSQL database (local or cloud — Supabase / Neon work great)