# 🔧 PostgreSQL Setup Guide - Windows

## Problem
PostgreSQL service shows "Running" but isn't actually listening on port 5432.

## Solution

### Option 1: Manual Service Restart (Recommended)

1. **Open Task Manager** (Ctrl + Shift + Esc)
2. Go to **Services** tab
3. Right-click **postgresql-x64-18** → **Restart**
4. Wait 5 seconds

Then in PowerShell (regular user, not admin needed):
```powershell
cd "C:\Users\NAM\Desktop\app-cham-cong"
& "C:\Program Files\PostgreSQL\18\bin\createdb.exe" -U postgres chamcong_db
```

---

### Option 2: Use PostgreSQL pgAdmin Interface

1. Open **pgAdmin 4** (should be installed with PostgreSQL)
2. Login with your PostgreSQL password
3. Right-click **Databases** → **Create** → **Database**
4. Name: `chamcong_db` → **Save**

---

### Option 3: Check PostgreSQL Installation

If it still doesn't work, PostgreSQL might be corrupted:

```powershell
# Check PostgreSQL directory
Get-ChildItem "C:\Program Files\PostgreSQL\18"

# Check data directory
Get-ChildItem "C:\Program Files\PostgreSQL\18\data" -Force
```

If data folder is missing or corrupted, reinstall PostgreSQL from:
https://www.postgresql.org/download/windows/

---

## Verify Connection

Once restarted, test the connection:

```powershell
# Should return success
& "C:\Program Files\PostgreSQL\18\bin\psql.exe" -U postgres -c "SELECT version();"

# Should work after database is created
& "C:\Program Files\PostgreSQL\18\bin\psql.exe" -U postgres -d chamcong_db -c "SELECT NOW();"
```

---

## Restart the App Server

Once database is created:

1. Press **Ctrl+C** in the server terminal
2. Run: `node server.js`
3. Visit http://localhost:3000
4. Database errors should be gone! ✅

---

## Troubleshooting

### Still getting ECONNREFUSED?
- PostgreSQL is definitely not running
- Try **Option 1** (Task Manager restart)
- Or reinstall PostgreSQL

### Getting "role postgres does not exist"?
- Use your actual PostgreSQL username instead of "postgres"
- Check during PostgreSQL installation what username you chose

### Getting "password authentication failed"?
- Update the password in `.env` file to match your PostgreSQL password

---

## Quick Reset (Nuclear Option)

If nothing works:

```powershell
# Uninstall PostgreSQL
# Reinstall from: https://www.postgresql.org/download/windows/
# During install, remember your password!
# Then create the database as shown above
```

Let me know when you've successfully created the database! 🎉
