A Beginner's Guide to Setting Up Prisma with PostgreSQL ๐Ÿš€

Prisma Setup and Configuration ๐Ÿ› ๏ธ

Initialize Prisma

Run this command:

npx prisma init

This creates a new folder called prisma in your project's root directory. Inside this folder, youโ€™ll find:

  • schema.prisma: This is where your database schema lives.

    • All your models (tables) will be defined here. โœ๏ธ

Sample User Model ๐Ÿ“‹

Here's an example of how to define a user table:

generator client {
  provider = "prisma-client-js"
  output   = "../src/generated/prisma"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        String   @id @default(uuid())
  name      String?  
  email     String   @unique
  image     String?  
  password  String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Environment File (.env) ๐ŸŒ

In your root directory, create a file called .env and add the following:

DATABASE_URL="postgresql://<username>:<password>@<host>:<port>/<database>"

โœจ Fun Part: How do you get this DATABASE_URL? Let's find out!


Steps to Obtain the Database URL ๐Ÿ”‘

Step 1: Pull the PostgreSQL Docker Image ๐Ÿณ

Run this command to download PostgreSQL:

docker pull postgres

Step 2: Run the PostgreSQL Container ๐Ÿƒ

Start a new container with:

docker run --name my_postgres_container -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypassword -e POSTGRES_DB=mydatabase -p 5433:5432 -d postgres

What do these flags mean? ๐Ÿง

  • --name my_postgres_container: Name your container for easy reference.

  • -e POSTGRES_USER=myuser: Set the PostgreSQL username.

  • -e POSTGRES_PASSWORD=mypassword: Set the PostgreSQL password.

  • -e POSTGRES_DB=mydatabase: Create a database named mydatabase.

  • -p 5433:5432: Map port 5432 inside the container to port 5433 on your system.

  • -d: Run the container in detached mode (in the background).

This setup makes PostgreSQL accessible on your local machine at port 5433. ๐ŸŽ‰

Step 3: Construct the DATABASE_URL ๐Ÿ”—

Your DATABASE_URL will look like this:

DATABASE_URL=postgresql://myuser:mypassword@localhost:5433/mydatabase

Add this to your .env file. โœ…


Prisma Client ๐Ÿค

Prisma Client lets you interact with your database directly in your code. ๐ŸŽฏ

  • Create a generator block in the Prisma schema file.

  • Define your models (like the User model shown earlier).


Managing Your Database with Migrations ๐Ÿ”„

Prisma migrations help you make changes to your database schema over time.

Create a Migration ๐Ÿ› ๏ธ

npx prisma migrate dev

This generates a migration file and applies it to your database. ๐Ÿ“

Deploy Migrations ๐ŸŒ

npx prisma migrate deploy

This applies any pending migrations to your database. โšก


Keep Your Schema Clean โœจ

Format your Prisma schema file with:

npx prisma format

๐Ÿ”ง Problem: Restarting the PostgreSQL Container After Shutdown

After shutting down my computer, the PostgreSQL container and the database become inactive and it is not working , so what should I do now ??


๐Ÿ’ก Solution: Reactivate the Container and Verify Database Accessibility

1๏ธโƒฃ Step 1: Start Docker

  • Ensure Docker is running on your computer. Open the Docker Desktop application or start the Docker service using your terminal. ๐Ÿ‹

2๏ธโƒฃ Step 2: Start the PostgreSQL Container

  • Run the following command to start your PostgreSQL container:
docker start my_postgres_container

Here, my_postgres_container is the name of the container you set earlier. Replace it if you named your container differently. ๐Ÿ› ๏ธ

Verify the container is running by executing:

docker ps

3๏ธโƒฃ Step 3: Verify the Database with Prisma Studio

  • Use Prisma Studio to confirm that your database is running and accessible visually. ๐ŸŽจ Run the following command:
npx prisma studio

This opens a GUI in your browser where you can:

  • View your tables and their data ๐Ÿ“Š

  • Confirm that the database schema and records are intact ๐Ÿ”

21
Subscribe to my newsletter

Read articles from Gaurav Kumar Maurya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gaurav Kumar Maurya
Gaurav Kumar Maurya