Initial commit from template
This commit is contained in:
157
.claude/commands/db-connect.md
Normal file
157
.claude/commands/db-connect.md
Normal file
@@ -0,0 +1,157 @@
|
||||
# PostgreSQL Datenbank Verbindung
|
||||
|
||||
Du verbindest dich mit der PostgreSQL Datenbank des Projekts.
|
||||
|
||||
## Automatischer Setup
|
||||
|
||||
### 1. Betriebssystem erkennen
|
||||
|
||||
Prüfe zuerst das Betriebssystem:
|
||||
|
||||
```bash
|
||||
uname -s
|
||||
```
|
||||
|
||||
### 2. PostgreSQL CLI installieren (falls nicht vorhanden)
|
||||
|
||||
**macOS:**
|
||||
```bash
|
||||
# Prüfen ob psql installiert ist
|
||||
which psql || brew install postgresql
|
||||
```
|
||||
|
||||
**Linux (Ubuntu/Debian):**
|
||||
```bash
|
||||
which psql || sudo apt-get update && sudo apt-get install -y postgresql-client
|
||||
```
|
||||
|
||||
**Linux (Alpine):**
|
||||
```bash
|
||||
which psql || apk add postgresql-client
|
||||
```
|
||||
|
||||
### 3. Environment Variable lesen
|
||||
|
||||
Lese die `DATABASE_URL` aus `.env.local` oder `.env`:
|
||||
|
||||
```bash
|
||||
# Aus .env.local
|
||||
grep DATABASE_URL .env.local 2>/dev/null || grep DATABASE_URL .env 2>/dev/null
|
||||
```
|
||||
|
||||
Die URL hat das Format:
|
||||
```
|
||||
postgresql://USER:PASSWORD@HOST:PORT/DATABASE
|
||||
```
|
||||
|
||||
### 4. Mit Datenbank verbinden
|
||||
|
||||
```bash
|
||||
# Direkte Verbindung mit URL
|
||||
psql "$DATABASE_URL"
|
||||
|
||||
# Oder mit Supabase
|
||||
psql "postgresql://postgres.[PROJECT-REF]:[PASSWORD]@aws-0-eu-central-1.pooler.supabase.com:6543/postgres"
|
||||
```
|
||||
|
||||
## Häufige Befehle nach Verbindung
|
||||
|
||||
```sql
|
||||
-- Alle Tabellen anzeigen
|
||||
\dt
|
||||
|
||||
-- Tabellen mit Schema
|
||||
\dt public.*
|
||||
|
||||
-- Tabellen-Details
|
||||
\d table_name
|
||||
|
||||
-- Alle Schemas
|
||||
\dn
|
||||
|
||||
-- Benutzer anzeigen
|
||||
\du
|
||||
|
||||
-- Aktuelle Datenbank
|
||||
SELECT current_database();
|
||||
|
||||
-- Tabellen-Größen
|
||||
SELECT
|
||||
tablename,
|
||||
pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename)) as size
|
||||
FROM pg_tables
|
||||
WHERE schemaname = 'public'
|
||||
ORDER BY pg_total_relation_size(schemaname || '.' || tablename) DESC;
|
||||
```
|
||||
|
||||
## Supabase spezifische Queries
|
||||
|
||||
```sql
|
||||
-- Auth Users (Supabase)
|
||||
SELECT id, email, created_at FROM auth.users LIMIT 10;
|
||||
|
||||
-- Storage Buckets
|
||||
SELECT * FROM storage.buckets;
|
||||
|
||||
-- RLS Policies
|
||||
SELECT * FROM pg_policies WHERE schemaname = 'public';
|
||||
|
||||
-- Enabled Extensions
|
||||
SELECT * FROM pg_extension;
|
||||
```
|
||||
|
||||
## SQL Datei ausführen
|
||||
|
||||
```bash
|
||||
# SQL Datei ausführen
|
||||
psql "$DATABASE_URL" -f migrations/001_init.sql
|
||||
|
||||
# Mit Output
|
||||
psql "$DATABASE_URL" -f migrations/001_init.sql -v ON_ERROR_STOP=1
|
||||
```
|
||||
|
||||
## Backup & Restore
|
||||
|
||||
```bash
|
||||
# Backup erstellen
|
||||
pg_dump "$DATABASE_URL" > backup.sql
|
||||
|
||||
# Nur Schema
|
||||
pg_dump "$DATABASE_URL" --schema-only > schema.sql
|
||||
|
||||
# Nur Daten
|
||||
pg_dump "$DATABASE_URL" --data-only > data.sql
|
||||
|
||||
# Restore
|
||||
psql "$DATABASE_URL" < backup.sql
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Connection refused
|
||||
- Supabase Projekt ist pausiert → Dashboard öffnen
|
||||
- Firewall blockiert Port 5432/6543
|
||||
- IP nicht in Allowlist (Supabase: Database Settings > Connection Pooling)
|
||||
|
||||
### SSL required
|
||||
```bash
|
||||
psql "$DATABASE_URL?sslmode=require"
|
||||
```
|
||||
|
||||
### Password authentication failed
|
||||
- Passwort in URL URL-encoded? (`@` → `%40`, etc.)
|
||||
- Richtiges Passwort aus Supabase Dashboard
|
||||
|
||||
---
|
||||
|
||||
## Automatische Verbindung
|
||||
|
||||
Führe diese Schritte aus:
|
||||
|
||||
1. **OS prüfen:** `uname -s`
|
||||
2. **psql prüfen:** `which psql`
|
||||
3. **Falls nicht vorhanden:** Installiere je nach OS
|
||||
4. **DATABASE_URL laden:** Aus .env.local oder .env
|
||||
5. **Verbinden:** `psql "$DATABASE_URL"`
|
||||
|
||||
Soll ich die Verbindung jetzt herstellen?
|
||||
Reference in New Issue
Block a user