# Quick Start: Deploy with Docker Compose PostgreSQL 🚀

## The Fastest Solution

Instead of fighting with your external PostgreSQL configuration, use the **included PostgreSQL container** that's already configured in your docker-compose.yml.

This approach:
- ✅ Works out of the box (no pg_hba.conf configuration needed)
- ✅ Runs PostgreSQL alongside your TaskFlow app
- ✅ Handles all SSL/authentication automatically
- ✅ Production-ready with persistent storage
- ✅ Easy to backup and manage

## How to Deploy

### Option 1: Using Docker Compose (Recommended)

```bash
# 1. Clone your repository on your production server
git clone https://your-repo-url/taskflow.git
cd taskflow

# 2. Create .env file (optional - uses defaults if not created)
cat > .env << EOF
POSTGRES_USER=taskflow
POSTGRES_PASSWORD=$(openssl rand -base64 32)
POSTGRES_DB=taskflow
APP_PORT=5000
EOF

# 3. Start everything
docker-compose up -d

# 4. Check logs
docker-compose logs -f app

# Expected output:
# serving on port 5000
# Checking database schema...
# ✓ Database schema is up to date
# ✓ Created default labels
# ✓ Database initialized successfully
```

### Option 2: Pull Pre-built Image

If you're using Drone CI to build images:

```bash
# 1. Create docker-compose.production.yml
cat > docker-compose.production.yml << 'EOF'
version: '3.8'

services:
  postgres:
    image: postgres:16-alpine
    container_name: taskflow-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-taskflow}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-taskflow_password}
      POSTGRES_DB: ${POSTGRES_DB:-taskflow}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - taskflow-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U taskflow"]
      interval: 10s
      timeout: 5s
      retries: 5

  app:
    image: registry.local.nothaft.cloud/taskflow:latest
    container_name: taskflow-app
    restart: unless-stopped
    environment:
      NODE_ENV: production
      DATABASE_URL: postgresql://${POSTGRES_USER:-taskflow}:${POSTGRES_PASSWORD:-taskflow_password}@postgres:5432/${POSTGRES_DB:-taskflow}
      PORT: 5000
    ports:
      - "5000:5000"
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - taskflow-network

volumes:
  postgres_data:
    driver: local

networks:
  taskflow-network:
    driver: bridge
EOF

# 2. Pull and start
docker pull registry.local.nothaft.cloud/taskflow:latest
docker-compose -f docker-compose.production.yml up -d

# 3. Verify
docker-compose -f docker-compose.production.yml logs -f app
```

## What This Gives You

### 1. Zero Configuration
- PostgreSQL is pre-configured for the TaskFlow app
- No pg_hba.conf editing required
- No SSL configuration needed
- Works immediately

### 2. Production Ready
- **Persistent Data**: PostgreSQL data stored in Docker volume
- **Automatic Backups**: Easy to backup the `postgres_data` volume
- **Health Checks**: Both app and database monitored
- **Auto-restart**: Containers restart on failure or reboot

### 3. Isolated Network
- App and database on private Docker network
- PostgreSQL not exposed to internet by default
- Secure communication between containers

## Data Persistence

Your PostgreSQL data is stored in a Docker volume:

```bash
# View volume
docker volume ls | grep postgres_data

# Backup database
docker exec taskflow-db pg_dump -U taskflow taskflow > backup.sql

# Backup volume
docker run --rm -v taskflow_postgres_data:/data -v $(pwd):/backup \
  alpine tar czf /backup/postgres-backup.tar.gz /data

# Restore volume
docker run --rm -v taskflow_postgres_data:/data -v $(pwd):/backup \
  alpine tar xzf /backup/postgres-backup.tar.gz -C /
```

## Accessing the Database

```bash
# Connect to PostgreSQL from host
docker exec -it taskflow-db psql -U taskflow -d taskflow

# Or using psql on host (if installed)
psql "postgresql://taskflow:taskflow_password@localhost:5432/taskflow"
```

## Exposing PostgreSQL (Optional)

If you need external access to PostgreSQL:

```yaml
# In docker-compose.yml, uncomment:
services:
  postgres:
    ports:
      - "5432:5432"  # Exposes PostgreSQL on host
```

**Warning**: Only expose if needed and use strong passwords!

## Monitoring

```bash
# View all logs
docker-compose logs -f

# View app logs only
docker-compose logs -f app

# View database logs only
docker-compose logs -f postgres

# Check container status
docker-compose ps

# Check resource usage
docker stats taskflow-app taskflow-db
```

## Scaling Considerations

### For Production Use:

1. **Use Strong Passwords**
   ```bash
   POSTGRES_PASSWORD=$(openssl rand -base64 32)
   ```

2. **Configure Backups**
   ```bash
   # Add to cron
   0 2 * * * docker exec taskflow-db pg_dump -U taskflow taskflow | gzip > /backups/taskflow-$(date +\%Y\%m\%d).sql.gz
   ```

3. **Monitor Resources**
   - Set memory limits in docker-compose.yml
   - Monitor disk usage for postgres_data volume

4. **Set Up Reverse Proxy** (nginx/Caddy)
   ```nginx
   server {
       listen 80;
       server_name taskflow.yourdomain.com;
       
       location / {
           proxy_pass http://localhost:5000;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
       }
   }
   ```

## Troubleshooting

### Container Won't Start

```bash
# Check logs
docker-compose logs

# Restart containers
docker-compose restart

# Rebuild if needed
docker-compose down
docker-compose up -d --build
```

### Database Connection Issues

```bash
# Check PostgreSQL is running
docker-compose ps postgres

# Check PostgreSQL logs
docker-compose logs postgres

# Test connection
docker exec taskflow-app npx tsx -e "
import { Pool } from 'pg';
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
pool.query('SELECT NOW()').then(r => console.log('Connected:', r.rows[0])).catch(e => console.error('Error:', e.message));
"
```

### Port Already in Use

```bash
# Change port in .env
echo "APP_PORT=8000" >> .env

# Restart
docker-compose up -d
```

## Migrating from External PostgreSQL

If you were using an external PostgreSQL and want to migrate:

```bash
# 1. Backup external database
pg_dump -h external-host -U taskflow -d taskflow > external-backup.sql

# 2. Start docker-compose PostgreSQL
docker-compose up -d postgres

# 3. Restore to new database
cat external-backup.sql | docker exec -i taskflow-db psql -U taskflow -d taskflow

# 4. Start app
docker-compose up -d app
```

## Why This Works

The included PostgreSQL container:
- Has **no SSL requirements** configured
- Has **permissive pg_hba.conf** (allows all local network connections)
- Runs on the **same Docker network** as your app
- Is **pre-configured** for the TaskFlow schema

## Next Steps

1. ✅ Deploy using docker-compose (see commands above)
2. ✅ Verify application is running
3. ✅ Set up backups
4. ✅ Configure reverse proxy/SSL (if exposing to internet)
5. ✅ Monitor and maintain

---

**This is the recommended approach** for getting your TaskFlow application running quickly in production. Once it's working, you can always migrate to an external PostgreSQL later if needed.
