Prisma ORM with PostgreSQL and Docker – Full Setup Guide (2025)
Posted by: Team Codeframer | @codeframerIntroduction
Looking to modernize your backend stack in 2025? Prisma ORM, when paired with PostgreSQL and Docker, provides a scalable, type-safe, and efficient database solution. Whether you're building with Next.js, Express, or any modern backend, this guide walks you through setting up Prisma ORM with PostgreSQL inside Docker containers.
In this blog, we’ll cover:
Why Prisma + PostgreSQL is a powerful combo
Setting up a Dockerized PostgreSQL instance
Installing and configuring Prisma ORM
Creating your first schema and running migrations
Why Prisma + PostgreSQL + Docker?
Prisma ORM is a next-generation TypeScript ORM that simplifies database access while ensuring type safety and performance. Pairing it with PostgreSQL, a reliable open-source database, and running it all in Docker ensures:
Consistent environments across teams
Easy setup and teardown
Portability for production deployment
Step 1: Create Your Project
Start by creating a new Node.js project:
1mkdir prisma-postgres-docker 2cd prisma-postgres-docker 3npm init -y
Install dependencies:
1npm install prisma @prisma/client
Step 2: Initialize Prisma
Run:
1npx prisma init
This creates a basic Prisma setup with:
.env
for environment variablesprisma/schema.prisma
for your data models
Step 3: Set Up PostgreSQL with Docker
Create a docker-compose.yml
file:
1version: '3.8' 2services: 3 db: 4 image: postgres:15 5 restart: always 6 environment: 7 POSTGRES_USER: postgres 8 POSTGRES_PASSWORD: password 9 POSTGRES_DB: mydb 10 ports: 11 - "5432:5432" 12 volumes: 13 - pgdata:/var/lib/postgresql/data 14 15volumes: 16 pgdata:
Now run:
1docker-compose up -d
This will spin up a PostgreSQL container with credentials you can configure in your .env
.
Step 4: Connect Prisma to Dockerized PostgreSQL
Update your .env
file:
1DATABASE_URL="postgresql://postgres:password@localhost:5432/mydb"
In your schema.prisma
file, update the datasource
block:
1datasource db { 2 provider = "postgresql" 3 url = env("DATABASE_URL") 4}
Step 5: Define Your First Prisma Model
Inside schema.prisma
, add:
1model User { 2 id String @id @default(uuid()) 3 email String @unique 4 name String? 5 createdAt DateTime @default(now()) 6}
Run the migration:
1npx prisma migrate dev --name init
This will:
Create a migration file
Apply it to your PostgreSQL DB
Generate Prisma Client
Step 6: Use Prisma Client in Your App
Create a prisma/client.ts
file:
1import { PrismaClient } from '@prisma/client'; 2const prisma = new PrismaClient(); 3export default prisma;
Now, use it anywhere:
1import prisma from './prisma/client'; 2async function main() { 3 const user = await prisma.user.create({ 4 data: { 5 email: 'test@example.com', 6 name: 'Lakshay', 7 }, 8 }); 9 console.log(user); 10} 11main();
Step 7: Useful Prisma CLI Commands
npx prisma studio
– GUI to view and edit DB recordsnpx prisma generate
– Regenerates Prisma clientnpx prisma format
– Autoformats the schema
Bonus: Dockerize the Entire Project (Optional)
Add a Dockerfile to containerize your app too:
1FROM node:20-alpine 2WORKDIR /app 3COPY package*.json ./ 4RUN npm install 5COPY . . 6CMD ["node", "index.js"]
Then, extend docker-compose.yml
to include the app container if needed.
Conclusion
With Prisma ORM, PostgreSQL, and Docker, you now have a clean, modern, and scalable backend setup perfect for projects in 2025. This stack is highly recommended for full-stack apps built with Next.js, Express, or NestJS.