# Quick Fix: Database Connection Error

## Problem
You're seeing: `Invalid API key` error

## Solution: Use Direct PostgreSQL Connection (2 minutes)

### Step 1: Get Database Connection String

1. Go to your **Supabase Dashboard**: https://app.supabase.com
2. Select your project
3. Go to **Settings** (gear icon) → **Database**
4. Scroll to **Connection String** section
5. Select **URI** tab
6. Copy the connection string (it looks like this):
   ```
   postgresql://postgres:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.co:5432/postgres
   ```

### Step 2: Replace Password

Replace `[YOUR-PASSWORD]` with your actual database password:
- If you don't know it, click **Reset database password** in Supabase
- Or use the password you set when creating the project

### Step 3: Add to .env File

Create or edit `backend/.env` file:

```env
# Direct PostgreSQL Connection (RECOMMENDED)
DATABASE_URL=postgresql://postgres:your_actual_password@db.your-project-ref.supabase.co:5432/postgres

# Server Configuration
PORT=3010
NODE_ENV=development

# CORS Configuration
CORS_ORIGIN=http://localhost:5173
```

**Example:**
```env
DATABASE_URL=postgresql://postgres:MySecurePassword123@db.abcdefghijklmnop.supabase.co:5432/postgres
```

### Step 4: Restart Server

```bash
cd backend
npm run dev
```

You should now see:
```
✅ Database connection successful (PostgreSQL direct)
```

## Why This Works

- Direct PostgreSQL connection bypasses Supabase API keys
- Faster for heavy SQL queries (which this system uses)
- More reliable for analytical queries
- No API key expiration issues

## Still Having Issues?

See `TROUBLESHOOTING.md` for detailed troubleshooting steps.

