43 lines
1.5 KiB
Bash
43 lines
1.5 KiB
Bash
#!/usr/bin/env bash
|
|
# 006-install-postgres - Install PostgreSQL (Debian 13 -> PG 17).
|
|
# Must run after 005: with /var/lib/postgresql already being the dedicated
|
|
# disk, the Debian installer creates the cluster directly on it - no
|
|
# data_directory changes needed.
|
|
# up: apt install postgresql + contrib, cluster started/enabled
|
|
# down: stop + purge packages and /etc/postgresql; data on disk is kept
|
|
set -euo pipefail
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
PGROOT=/var/lib/postgresql
|
|
|
|
case "${1:-}" in
|
|
up)
|
|
if command -v pg_lsclusters >/dev/null 2>&1 && pg_lsclusters --no-header | grep -q .; then
|
|
echo " postgres cluster already present - skipping"
|
|
exit 0
|
|
fi
|
|
apt-get update -qq
|
|
apt-get install -y postgresql postgresql-contrib >/dev/null
|
|
chown postgres:postgres "$PGROOT"
|
|
systemctl enable --now postgresql >/dev/null
|
|
pg_isready -q
|
|
pg_lsclusters --no-header | while read -r _ _ _ _ _ datadir _; do
|
|
if findmnt -rn "$datadir" >/dev/null; then
|
|
echo " cluster data on dedicated disk: $datadir"
|
|
else
|
|
echo " WARNING: $datadir is NOT on the dedicated disk" >&2
|
|
fi
|
|
done
|
|
pg_lsclusters --no-header
|
|
;;
|
|
down)
|
|
{ pg_lsclusters --no-header 2>/dev/null || true; } | while read -r ver name _; do
|
|
pg_ctlcluster "$ver" "$name" stop 2>/dev/null || true
|
|
done
|
|
apt-get purge -y 'postgresql*' >/dev/null
|
|
apt-get autoremove --purge -y >/dev/null
|
|
rm -rf /etc/postgresql
|
|
echo " postgres stopped + packages purged (data kept under $PGROOT)"
|
|
;;
|
|
esac
|