# Quick Start: PostgreSQL with Jellyfin

## 🚀 Fastest Setup Ever (2 Steps!)

### Step 1: Create PostgreSQL User

```bash
psql -U postgres -c "CREATE USER jellyfin WITH PASSWORD 'your_password' CREATEDB;"
```

### Step 2: Configure Jellyfin

Create/Edit `config/database.xml`:

```xml
<?xml version="1.0" encoding="utf-8"?>
<DatabaseConfigurationOptions>
  <DatabaseType>Jellyfin-PostgreSQL</DatabaseType>
  <LockingBehavior>NoLock</LockingBehavior>
  <CustomProviderOptions>
    <Options>
      <CustomDatabaseOption>
        <Key>host</Key>
        <Value>localhost</Value>
      </CustomDatabaseOption>
      <CustomDatabaseOption>
        <Key>port</Key>
        <Value>5432</Value>
      </CustomDatabaseOption>
      <CustomDatabaseOption>
        <Key>database</Key>
        <Value>jellyfin</Value>
      </CustomDatabaseOption>
      <CustomDatabaseOption>
        <Key>username</Key>
        <Value>jellyfin</Value>
      </CustomDatabaseOption>
      <CustomDatabaseOption>
        <Key>password</Key>
        <Value>your_password</Value>
      </CustomDatabaseOption>
    </Options>
  </CustomProviderOptions>
</DatabaseConfigurationOptions>
```

### Step 3: Start Jellyfin

```bash
./jellyfin
```

**That's it!** Jellyfin automatically:
- ✅ Creates the database
- ✅ Creates all 30 tables
- ✅ Sets up indexes
- ✅ Grants permissions

## What Jellyfin Does Automatically

```
Starting Jellyfin...
  ↓
Checking if database 'jellyfin' exists...
  ↓
Database not found - Creating...
  ↓
✅ Database created!
✅ Permissions granted!
  ↓
Applying migrations...
  ↓
✅ All 30 tables created!
  ↓
Jellyfin ready!
```

## Docker Compose

```yaml
version: '3'
services:
  postgres:
    image: postgres:15
    environment:
      POSTGRES_USER: jellyfin
      POSTGRES_PASSWORD: secure_password
      # No POSTGRES_DB needed - Jellyfin creates it!
    volumes:
      - postgres_data:/var/lib/postgresql/data

  jellyfin:
    image: jellyfin/jellyfin:latest
    depends_on:
      - postgres
    volumes:
      - jellyfin_config:/config
    # Configure PostgreSQL via config/database.xml

volumes:
  postgres_data:
  jellyfin_config:
```

## Verification

### Check Database Exists

```bash
psql -U jellyfin -l | grep jellyfin
# Output: jellyfin | jellyfin | UTF8 | ...
```

### Check Tables

```bash
psql -U jellyfin -d jellyfin -c "\dt"
# Output: List of 30 tables
```

### Check Logs

```bash
tail -f <jellyfin-log-directory>/log_*.txt | grep PostgreSQL
```

Expected:
```
[INFO] Checking if PostgreSQL database exists...
[INFO] PostgreSQL database 'jellyfin' does not exist. Creating...
[INFO] PostgreSQL database 'jellyfin' created successfully
```

## Common Issues

### ❌ "permission denied to create database"

**Fix:**
```sql
ALTER USER jellyfin CREATEDB;
```

### ❌ "role 'jellyfin' does not exist"

**Fix:** Create the user (Step 1)

### ❌ "could not connect to server"

**Fix:** 
- Check PostgreSQL is running: `systemctl status postgresql`
- Check host/port in config
- Check firewall

## Pro Tips

### Production Security

After first successful start, you can revoke CREATEDB:
```sql
ALTER USER jellyfin WITH NOCREATEDB;
```

### Custom Database Name

Want to use a different database name?
```xml
<CustomDatabaseOption>
  <Key>database</Key>
  <Value>my_custom_jellyfin_db</Value>
</CustomDatabaseOption>
```

### Multiple Jellyfin Instances

Each instance needs its own database:
```sql
CREATE USER jellyfin1 WITH PASSWORD 'pass1' CREATEDB;
CREATE USER jellyfin2 WITH PASSWORD 'pass2' CREATEDB;
```

Configure each with different database names:
- Instance 1: `database=jellyfin1`
- Instance 2: `database=jellyfin2`

## Full Documentation

- 📖 [DATABASE_CONFIGURATION.md](DATABASE_CONFIGURATION.md) - Complete setup guide
- 📖 [AUTOMATIC_DATABASE_CREATION.md](AUTOMATIC_DATABASE_CREATION.md) - Feature details
- 📖 [POSTGRESQL_TROUBLESHOOTING.md](POSTGRESQL_TROUBLESHOOTING.md) - Problem solving

## Summary

**Before:** 6 manual SQL commands + configuration  
**Now:** 1 SQL command + configuration  

**That's 83% less work!** 🎉
