# A Beginner's Guide to Setting Up Prisma with PostgreSQL 🚀

## Prisma Setup and Configuration 🛠️

### Initialize Prisma

Run this command:

```javascript
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:

```javascript
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:

```bash
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:

```bash
docker pull postgres
```

### Step 2: Run the PostgreSQL Container 🏃

Start a new container with:

```bash
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:

```javascript
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 🛠️

```bash
npx prisma migrate dev
```

This generates a migration file and applies it to your database. 📝

### Deploy Migrations 🌐

```bash
npx prisma migrate deploy
```

This applies any pending migrations to your database. ⚡

---

## Keep Your Schema Clean ✨

Format your Prisma schema file with:

```bash
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:
    

```bash
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:

```bash
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:
    

```bash
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 🔍
