DEV Community

Manendra Verma
Manendra Verma

Posted on

How to Setup NestJS with Prisma and PostgreSQL (2026 Complete Guide)

Step-1: Create NestJS Project

Install the NestJS CLI and create a new project:

npm install -g @nestjs/cli
Enter fullscreen mode Exit fullscreen mode
nest new nestjs-prisma
Enter fullscreen mode Exit fullscreen mode

When prompted, select npm as your package manager. Navigate to the project directory:

cd nestjs-prisma
Enter fullscreen mode Exit fullscreen mode

Step-2: Setup Prisma

Install Dependencies: Install the necessary Prisma packages and database drivers:

npm install prisma --save-dev
Enter fullscreen mode Exit fullscreen mode
npm install @prisma/client @prisma/adapter-pg pg
Enter fullscreen mode Exit fullscreen mode

Step-2.1: Initialize Prisma

Initialize Prisma in your project:

npx prisma init --output ../src/generated/prisma
Enter fullscreen mode Exit fullscreen mode

It will also set the client output from "../generated/prisma” to “../src/generated/prisma”

Step-2.2: Set the generator output path

Note: Specify your output path for the generated Prisma client by either passing --output ../src/generated/prisma during prisma init

generator client {
  provider = "prisma-client"
  output   = "../src/generated/prisma"
}
Enter fullscreen mode Exit fullscreen mode

Step-2.3: Configure the module format #

generator client {
  provider        = "prisma-client"
  output          = "../src/generated/prisma"
  moduleFormat    = "cjs"
}
Enter fullscreen mode Exit fullscreen mode

Note: The moduleFormat configuration is required because Prisma v7 ships as an ES module by default, which does not work with NestJS's CommonJS setup. Setting moduleFormat to cjs forces Prisma to generate a CommonJSmodule instead of ESM.

Step-3: Configure PostgreSQL Connection

Edit .env

DATABASE_URL="postgresql://postgres:password@localhost:5432/nestdb?schema=public"
Enter fullscreen mode Exit fullscreen mode

Step-4: Define your data model

Add the following two models to your schema.prisma file:

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  posts Post[]
}
model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean? @default(false)
  author    User?    @relation(fields: [authorId], references: [id])
  authorId  Int?
}
Enter fullscreen mode Exit fullscreen mode

Step-5: Run Migration

npx prisma migrate dev --name init
Enter fullscreen mode Exit fullscreen mode

Step-6: Generate Prisma Client

Once installed, you can run the generate command to generate the types and Client needed for your project.

npx prisma generate
Enter fullscreen mode Exit fullscreen mode

Step-7: Create a Prisma service

nest g module prisma
Enter fullscreen mode Exit fullscreen mode
nest g service prisma
Enter fullscreen mode Exit fullscreen mode

Inside the src directory, create a new file called prisma.service.ts and add the following code to it:


import { Injectable } from "@nestjs/common";
import { PrismaClient } from "../generated/prisma/client.js";
import { PrismaPg } from "@prisma/adapter-pg";

@Injectable()
export class PrismaService extends PrismaClient {
  constructor() {
    const adapter = new PrismaPg({
      connectionString: process.env.DATABASE_URL as string,
    });
    super({ adapter });
  }
}
Enter fullscreen mode Exit fullscreen mode

Step-8: Register services in the app module (Optional)

Remember to register the new services in the app module.

Optional: If you create a module for prisma service. Otherwise paste below code.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaService } from './prisma/prisma.service';

@Module({
  imports: [PrismaModule],
  controllers: [AppController],
  providers: [AppService, PrismaService],
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Step-9: Test Setup

npm run start
or
npm run start:dev
Enter fullscreen mode Exit fullscreen mode

Step-10: Create services

Now create services and use it like you do in normal Nest app.

----- 📄 Document End 🎉 -----

Top comments (0)