- Tipo: patch - Database backup: database-backups/ids_v1.0.3_20251117_160011.sql.gz - Data: 2025-11-17 16:00:18
168 lines
4.8 KiB
Markdown
168 lines
4.8 KiB
Markdown
# 🔧 Risoluzione Errore PostgreSQL
|
|
|
|
## Errore Riscontrato
|
|
|
|
```
|
|
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed:
|
|
FATAL: Ident authentication failed for user "ids_user"
|
|
```
|
|
|
|
## Causa
|
|
|
|
PostgreSQL su AlmaLinux 9 di default usa autenticazione **"ident"** invece di password. Python (psycopg2) non può connettersi con password.
|
|
|
|
## Soluzione (3 minuti)
|
|
|
|
### Step 1: Esegui Script di Fix
|
|
|
|
```bash
|
|
cd /opt/ids/deployment
|
|
chmod +x fix_postgresql_auth.sh
|
|
./fix_postgresql_auth.sh
|
|
```
|
|
|
|
**Output atteso:**
|
|
```
|
|
╔═══════════════════════════════════════════════╗
|
|
║ PostgreSQL Authentication Fix ║
|
|
╚═══════════════════════════════════════════════╝
|
|
|
|
📂 File pg_hba.conf: /var/lib/pgsql/data/pg_hba.conf
|
|
|
|
💾 Backup configurazione...
|
|
✅ Backup salvato: /var/lib/pgsql/data/pg_hba.conf.backup_20250117_123456
|
|
|
|
📋 Configurazione ATTUALE:
|
|
local all all ident
|
|
host all all 127.0.0.1/32 ident
|
|
host all all ::1/128 ident
|
|
|
|
🔧 Modifico pg_hba.conf...
|
|
✅ Configurazione modificata
|
|
|
|
📋 Configurazione NUOVA:
|
|
local all all scram-sha-256
|
|
host all all 127.0.0.1/32 scram-sha-256
|
|
host all all ::1/128 scram-sha-256
|
|
|
|
🔄 Ricarico configurazione PostgreSQL...
|
|
✅ PostgreSQL ricaricato
|
|
|
|
🧪 Test connessione con password...
|
|
✅ Connessione RIUSCITA!
|
|
PostgreSQL ora accetta autenticazione con password
|
|
|
|
╔═══════════════════════════════════════════════╗
|
|
║ ✅ FIX COMPLETATO ║
|
|
╚═══════════════════════════════════════════════╝
|
|
```
|
|
|
|
### Step 2: Riavvia Syslog Parser
|
|
|
|
```bash
|
|
# Stop processo esistente
|
|
pkill -f syslog_parser
|
|
|
|
# Avvia nuovamente
|
|
cd /opt/ids/python_ml
|
|
sudo -u ids nohup python3.11 syslog_parser.py > /var/log/ids/syslog_parser.log 2>&1 &
|
|
```
|
|
|
|
### Step 3: Verifica Funzionamento
|
|
|
|
```bash
|
|
# Verifica processo attivo
|
|
ps aux | grep syslog_parser
|
|
|
|
# Monitora log (NON dovrebbero esserci più errori)
|
|
tail -f /var/log/ids/syslog_parser.log
|
|
```
|
|
|
|
**Output atteso:**
|
|
```
|
|
2025-01-17 12:34:56 - INFO - Syslog Parser avviato
|
|
2025-01-17 12:34:56 - INFO - Connessione database riuscita
|
|
2025-01-17 12:34:56 - INFO - In ascolto su /var/log/mikrotik/raw.log
|
|
2025-01-17 12:34:57 - INFO - Log processato: 192.168.1.1 → 8.8.8.8:53
|
|
```
|
|
|
|
### Step 4: Verifica Database Si Popola
|
|
|
|
```bash
|
|
# Conta log nel database
|
|
psql -U ids_user -d ids_database -c "SELECT COUNT(*) FROM network_logs;"
|
|
|
|
# Mostra ultimi 5 log
|
|
psql -U ids_user -d ids_database -c "SELECT * FROM network_logs ORDER BY timestamp DESC LIMIT 5;"
|
|
```
|
|
|
|
## Cosa Fa lo Script
|
|
|
|
1. **Trova pg_hba.conf** automaticamente
|
|
2. **Backup** configurazione originale
|
|
3. **Modifica** autenticazione:
|
|
- `ident` → `scram-sha-256` (password-based)
|
|
4. **Ricarica** PostgreSQL (senza restart)
|
|
5. **Testa** connessione con password da `.env`
|
|
|
|
## Verifica Manuale (Opzionale)
|
|
|
|
Se vuoi verificare manualmente:
|
|
|
|
```bash
|
|
# Test connessione PostgreSQL con password
|
|
psql -h localhost -U ids_user -d ids_database -c "SELECT 1;"
|
|
|
|
# Se richiede password, inserisci quella in /opt/ids/.env (PGPASSWORD)
|
|
```
|
|
|
|
## Rollback (Se Necessario)
|
|
|
|
Se qualcosa va storto, ripristina backup:
|
|
|
|
```bash
|
|
# Lista backup disponibili
|
|
ls -la /var/lib/pgsql/data/pg_hba.conf.backup_*
|
|
|
|
# Ripristina ultimo backup
|
|
ULTIMO_BACKUP=$(ls -t /var/lib/pgsql/data/pg_hba.conf.backup_* | head -1)
|
|
cp "$ULTIMO_BACKUP" /var/lib/pgsql/data/pg_hba.conf
|
|
|
|
# Ricarica PostgreSQL
|
|
systemctl reload postgresql
|
|
```
|
|
|
|
## Note Tecniche
|
|
|
|
**File modificato:**
|
|
- `/var/lib/pgsql/data/pg_hba.conf` (su AlmaLinux 9)
|
|
- `/etc/postgresql/15/main/pg_hba.conf` (su Debian/Ubuntu)
|
|
|
|
**Modifiche effettuate:**
|
|
```diff
|
|
- local all all ident
|
|
+ local all all scram-sha-256
|
|
|
|
- host all all 127.0.0.1/32 ident
|
|
+ host all all 127.0.0.1/32 scram-sha-256
|
|
|
|
- host all all ::1/128 ident
|
|
+ host all all ::1/128 scram-sha-256
|
|
```
|
|
|
|
**Metodi autenticazione PostgreSQL:**
|
|
- `ident`: Usa nome utente sistema operativo (non password)
|
|
- `md5`: Password MD5 hash (deprecato)
|
|
- `scram-sha-256`: Password SHA-256 hash (✅ raccomandato, sicuro)
|
|
|
|
## Prevenzione Futura
|
|
|
|
Questo fix è **già incluso** nella guida aggiornata:
|
|
- `deployment/INSTALLAZIONE_STEP_BY_STEP.md` → Step 12b
|
|
|
|
Nuove installazioni non avranno questo problema.
|
|
|
|
---
|
|
|
|
**Problema risolto! 🎉**
|