# πŸ”§ 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! πŸŽ‰**