#!/bin/bash set -e DB_NAME="mitm_db" DB_USER="mitm_user" DB_PASS="mitm_password" LAN_SUBNET="192.168.178.0/24" echo "[1] Installing PostgreSQL…" sudo apt update -y sudo apt install -y postgresql postgresql-contrib echo "[2] Configuring PostgreSQL to listen on all addresses…" PG_CONF="/etc/postgresql/$(ls /etc/postgresql)/main/postgresql.conf" sudo sed -i "s/^#listen_addresses =.*/listen_addresses = '*'/" "$PG_CONF" echo "[3] Updating pg_hba.conf for LAN + localhost access…" PG_HBA="/etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf" # Add localhost if ! grep -Eq "^[ ]*host[ ]+all[ ]+all[ ]+127.0.0.1/32" "$PG_HBA"; then echo "host all all 127.0.0.1/32 md5" | sudo tee -a "$PG_HBA" fi # Add LAN if ! grep -q "$LAN_SUBNET" "$PG_HBA"; then echo "host all all $LAN_SUBNET md5" | sudo tee -a "$PG_HBA" fi echo "[4] Restarting PostgreSQL…" sudo systemctl restart postgresql echo "[5] Creating database + user (idempotent)…" sudo -u postgres psql <