Tillitsdone
down Scroll to discover

Set Up Prisma with PostgreSQL in Node.js

Learn how to integrate Prisma ORM with PostgreSQL in your Node.js application.

A step-by-step guide covering installation, database configuration, schema modeling, and basic CRUD operations.
thumbnail

Setting Up Prisma with PostgreSQL in Node.js: A Practical Guide

Abstract digital landscape with flowing data streams in bright fuchsia and lime colors representing database connections and data flow geometric patterns high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Are you ready to supercharge your Node.js application with a powerful database setup? Today, we’ll walk through setting up Prisma with PostgreSQL - a combination that’s becoming increasingly popular among developers for its type safety and intuitive query building.

Prerequisites

Before we dive in, make sure you have:

  • Node.js installed on your system
  • PostgreSQL server running locally
  • Basic understanding of JavaScript/Node.js

Getting Started

First, let’s create a new Node.js project:

Terminal window
mkdir prisma-postgres-demo
cd prisma-postgres-demo
npm init -y

Abstract cosmic garden with crystalline structures in shimmering yellow and lime colors floating geometric shapes energy particles high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Installing Dependencies

Let’s install the necessary packages:

Terminal window
npm install @prisma/client
npm install prisma --save-dev

Initializing Prisma

Initialize Prisma in your project:

Terminal window
npx prisma init

Setting Up Database Connection

Update your .env file with your PostgreSQL connection URL:

DATABASE_URL="postgresql://username:password@localhost:5432/mydb?schema=public"

Creating Your First Model

In your schema.prisma file, define your first model:

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
createdAt DateTime @default(now())
}

Fluid abstract texture with interweaving waves in fuchsia and yellow colors representing data flow patterns dynamic energy streams high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Migrating Your Database

Run your first migration:

Terminal window
npx prisma migrate dev --name init

Using Prisma Client

Create a new file index.js:

const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
async function main() {
const user = await prisma.user.create({
data: {
email: 'test@example.com',
name: 'Test User'
}
})
console.log(user)
}
main()
.catch(e => console.error(e))
.finally(async () => await prisma.$disconnect())

And that’s it! You now have a fully functional Prisma setup with PostgreSQL in your Node.js application. Start building your next great project with confidence!

Ethereal digital planet surface with geometric patterns in lime and shimmering fuchsia colors data streams flowing through crystalline structures high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.