#!/usr/bin/env bash # ADPanel privileged job: fully disable and remove pgAdmin4 + PostgreSQL. # Invoked via adpanel-priv: adpanel-priv job pgadmin-disable [listen-port] set -u export DEBIAN_FRONTEND=noninteractive systemctl stop postgresql 2>/dev/null || true systemctl disable postgresql 2>/dev/null || true pkill -9 -x postgres 2>/dev/null || true # Remove the ADPanel-managed remote-bind pg_hba.conf rules installed by # pg-add-hba (marker-delimited block) plus any legacy untagged allow lines # from older panel versions. Only those lines are removed; PostgreSQL # packages and data are deliberately NOT purged here — the panel's database # management feature (type=postgresql) can reuse the same instance and a # purge would destroy user databases. # pg_hba.conf lives at /etc/postgresql///pg_hba.conf on # Debian — the same recursive layout pg-add-hba writes to — so locate the # files the same way instead of a single-level glob. while IFS= read -r hba; do [ -f "$hba" ] || continue sed -i \ -e '/^# BEGIN ADPanel-managed remote-bind rules (added by adpanel-priv pg-add-hba)$/,/^# END ADPanel-managed remote-bind rules$/d' \ -e '/^host all all 0\.0\.0\.0\/0 scram-sha-256$/d' \ -e '/^host all all ::0\/0 scram-sha-256$/d' \ "$hba" 2>/dev/null || true done < <(find /etc/postgresql -name pg_hba.conf 2>/dev/null) # Only if PostgreSQL is somehow still active (it was just stopped above), # reload so the removed rules take effect; otherwise leave the on-disk # config clean for the next start. if systemctl is-active --quiet postgresql 2>/dev/null; then systemctl reload postgresql 2>/dev/null || true fi systemctl stop apache2 2>/dev/null || true systemctl disable apache2 2>/dev/null || true pkill -9 -x apache2 2>/dev/null || true rm -f /etc/apache2/sites-enabled/pgadmin4.conf 2>/dev/null || true rm -f /etc/apache2/sites-available/pgadmin4.conf 2>/dev/null || true rm -f /etc/apache2/conf-enabled/pgadmin4.conf 2>/dev/null || true rm -f /etc/apache2/conf-available/pgadmin4.conf 2>/dev/null || true # Remove Listen 5050 from ports.conf (clean slate for reinstall); if no # listener remains, leave a placeholder comment (nginx owns port 80). sed -i '/Listen 5050/d' /etc/apache2/ports.conf 2>/dev/null || true if ! grep -q '^Listen' /etc/apache2/ports.conf 2>/dev/null; then echo '# Apache ports - no active listeners (nginx handles web traffic)' > /etc/apache2/ports.conf fi # Placeholder keeps nginx include lines valid without serving pgAdmin. echo '# pgAdmin4 disabled - placeholder to prevent nginx include errors' > /etc/nginx/snippets/pgadmin4.conf 2>/dev/null || true for conf in /etc/nginx/sites-available/*.conf /etc/nginx/sites-enabled/*.conf /etc/nginx/conf.d/*.conf; do if [ -f "$conf" ]; then sed -i '/include.*pgadmin4.conf/d' "$conf" 2>/dev/null || true fi done nginx -t 2>/dev/null && systemctl reload nginx 2>/dev/null || true apt-get remove --purge -y pgadmin4-web pgadmin4-desktop pgadmin4 2>/dev/null || true apt-get autoremove -y 2>/dev/null || true rm -rf /var/lib/pgadmin /var/log/pgadmin /etc/pgadmin /usr/pgadmin4 2>/dev/null || true rm -f /etc/apt/sources.list.d/pgadmin4.list 2>/dev/null || true rm -f /usr/share/keyrings/packages-pgadmin-org.gpg 2>/dev/null || true apt-get update -qq 2>/dev/null || true # Optional: remove the firewall rule for the former PostgreSQL port. PORT="${1:-}" if [ -n "$PORT" ] && [ "$PORT" = "${PORT//[!0-9]/}" ] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; then ufw delete allow "${PORT}/tcp" 2>/dev/null || true iptables -D INPUT -p tcp --dport "$PORT" -j ACCEPT 2>/dev/null || true fi true