# El Captain Sports Only Database

## Connection Details

| Key | Value |
|-----|-------|
| **Host (public)** | `18.192.166.221` |
| **Port (public)** | `5434` |
| **Host (internal/CapRover apps)** | `srv-captain--elcaptainsportsonlydb` |
| **Port (internal)** | `5432` |
| **User** | `elcaptain` |
| **Password** | `ElCaptain2024!` |
| **Default DB** | `elcaptainsportsonly` |
| **Superuser** | `postgres` |
| **Superuser Password** | `Alarcade123#` |
| **Version** | PostgreSQL 16.14 (Alpine) |
| **Docker Service** | `srv-captain--elcaptainsportsonlydb` |
| **Storage** | Persistent volume: `captain--elcaptainsportsonlydb-data` |

---

## Connection Strings

```bash
# External apps (public internet)
postgresql://elcaptain:ElCaptain2024!@18.192.166.221:5434/elcaptainsportsonly

# CapRover apps (internal Docker network)
postgresql://elcaptain:ElCaptain2024!@srv-captain--elcaptainsportsonlydb:5432/elcaptainsportsonly

# Superuser (external)
postgresql://postgres:Alarcade123#@18.192.166.221:5434/postgres
```

---

## Quick Connect

### psql (CLI)
```bash
PGPASSWORD='ElCaptain2024!' psql -h 18.192.166.221 -p 5434 -U elcaptain -d elcaptainsportsonly
```

### Python (psycopg2)
```python
import psycopg2

conn = psycopg2.connect(
    host="18.192.166.221",
    port=5434,
    user="elcaptain",
    password="ElCaptain2024!",
    dbname="elcaptainsportsonly"
)
```

### Node.js (pg)
```javascript
const { Pool } = require('pg');

const pool = new Pool({
  host: '18.192.166.221',
  port: 5434,
  user: 'elcaptain',
  password: 'ElCaptain2024!',
  database: 'elcaptainsportsonly',
  ssl: false
});
```

### Go
```go
dsn := "host=18.192.166.221 port=5434 user=elcaptain password=ElCaptain2024! dbname=elcaptainsportsonly sslmode=disable"
```

### C# / Unity
```csharp
string connStr = "Host=18.192.166.221;Port=5434;Username=elcaptain;Password=ElCaptain2024!;Database=elcaptainsportsonly";
```

### Laravel (.env)
```env
DB_CONNECTION=pgsql
DB_HOST=18.192.166.221
DB_PORT=5434
DB_DATABASE=elcaptainsportsonly
DB_USERNAME=elcaptain
DB_PASSWORD=ElCaptain2024!
```

### Flutter (Dart)
```dart
final connection = await Connection.open(Endpoint(
  host: '18.192.166.221',
  port: 5434,
  database: 'elcaptainsportsonly',
  username: 'elcaptain',
  password: 'ElCaptain2024!',
));
```

---

## Admin Operations

### Create a New Database
```sql
CREATE DATABASE my_new_db;
```

### Create a New User (for an app)
```sql
CREATE USER myapp_user WITH PASSWORD 'secure_password_here';
GRANT ALL PRIVILEGES ON DATABASE my_new_db TO myapp_user;
```

### Create a Read-Only User
```sql
CREATE USER readonly_user WITH PASSWORD 'readonly_pass';
GRANT CONNECT ON DATABASE elcaptainsportsonly TO readonly_user;
GRANT USAGE ON SCHEMA public TO readonly_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_user;
```

### List All Databases
```sql
\l
-- or
SELECT datname FROM pg_database WHERE datistemplate = false;
```

### List All Tables
```sql
\dt
-- or
SELECT tablename FROM pg_tables WHERE schemaname = 'public';
```

### Check Database Size
```sql
SELECT pg_size_pretty(pg_database_size('elcaptainsportsonly'));
```

### Check All Database Sizes
```sql
SELECT datname, pg_size_pretty(pg_database_size(datname)) 
FROM pg_database WHERE datistemplate = false ORDER BY pg_database_size(datname) DESC;
```

---

## Useful Extensions

```sql
-- Enable UUID generation
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Enable full-text search extras
CREATE EXTENSION IF NOT EXISTS pg_trgm;

-- Enable crypto functions
CREATE EXTENSION IF NOT EXISTS pgcrypto;

-- Check available extensions
SELECT name, comment FROM pg_available_extensions ORDER BY name;
```

---

## SSH Access (Server-Side)

```bash
# SSH to server
ssh -i /Users/mahmoudaglan/NewMigration/newServer.pem -o StrictHostKeyChecking=no ubuntu@18.192.166.221

# Connect to DB from inside container
sudo docker exec -it $(sudo docker ps -q --filter "name=srv-captain--elcaptainsportsonlydb") psql -U elcaptain -d elcaptainsportsonly

# Connect as superuser
sudo docker exec -it $(sudo docker ps -q --filter "name=srv-captain--elcaptainsportsonlydb") psql -U postgres

# View logs
sudo docker logs $(sudo docker ps -q --filter "name=srv-captain--elcaptainsportsonlydb") --tail 50

# Restart
sudo docker service update --force srv-captain--elcaptainsportsonlydb

# Backup a database
sudo docker exec $(sudo docker ps -q --filter "name=srv-captain--elcaptainsportsonlydb") pg_dump -U elcaptain elcaptainsportsonly > backup.sql

# Restore a database
cat backup.sql | sudo docker exec -i $(sudo docker ps -q --filter "name=srv-captain--elcaptainsportsonlydb") psql -U elcaptain -d elcaptainsportsonly
```

---

## Notes

- `elcaptain` user owns the `elcaptainsportsonly` database and has full privileges on it.
- `postgres` superuser can create databases, users, extensions, and manage everything.
- Port `5434` is exposed publicly — any app on the internet can connect with the credentials.
- For CapRover apps, use the internal hostname `srv-captain--elcaptainsportsonlydb` on port `5432` (faster, no SSL needed).
- Data persists across container restarts via the Docker volume `captain--elcaptainsportsonlydb-data`.
- Image pinned to `postgres:16-alpine` — do NOT use `postgres:latest` (v18+ breaks with the `/var/lib/postgresql/data` mount path).
