Update tools.func

This commit is contained in:
CanbiZ
2025-10-14 21:02:11 +02:00
parent a0ee5c315f
commit 96a4d05e71

View File

@@ -1777,48 +1777,30 @@ function setup_postgresql() {
if command -v psql >/dev/null; then if command -v psql >/dev/null; then
CURRENT_PG_VERSION="$(psql -V | awk '{print $3}' | cut -d. -f1)" CURRENT_PG_VERSION="$(psql -V | awk '{print $3}' | cut -d. -f1)"
if [[ "$CURRENT_PG_VERSION" == "$PG_VERSION" ]]; then [[ "$CURRENT_PG_VERSION" != "$PG_VERSION" ]] && NEED_PG_INSTALL=true
: # PostgreSQL is already at the desired version
else
msg_info "Detected PostgreSQL $CURRENT_PG_VERSION, preparing upgrade to $PG_VERSION"
NEED_PG_INSTALL=true
fi
else else
NEED_PG_INSTALL=true NEED_PG_INSTALL=true
fi fi
if [[ "$NEED_PG_INSTALL" == true ]]; then if [[ "$NEED_PG_INSTALL" == true ]]; then
msg_info "Installing PostgreSQL $PG_VERSION"
if [[ -n "$CURRENT_PG_VERSION" ]]; then if [[ -n "$CURRENT_PG_VERSION" ]]; then
msg_info "Dumping PostgreSQL $CURRENT_PG_VERSION data" $STD su - postgres -c "pg_dumpall > /var/lib/postgresql/backup_$(date +%F)_v${CURRENT_PG_VERSION}.sql"
su - postgres -c "pg_dumpall > /var/lib/postgresql/backup_$(date +%F)_v${CURRENT_PG_VERSION}.sql" $STD systemctl stop postgresql
msg_ok "Data dump completed"
systemctl stop postgresql
fi fi
rm -f /etc/apt/sources.list.d/pgdg.* /etc/apt/keyrings/postgresql.gpg rm -f /etc/apt/sources.list.d/pgdg.* /etc/apt/keyrings/postgresql.gpg
msg_info "Adding PostgreSQL PGDG repository"
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc |
gpg --dearmor -o /etc/apt/keyrings/postgresql.gpg gpg --dearmor -o /etc/apt/keyrings/postgresql.gpg
# Fallback logic for PostgreSQL repos
local SUITE="$DISTRO_CODENAME" local SUITE="$DISTRO_CODENAME"
if ! curl -fsSL "https://apt.postgresql.org/pub/repos/apt/dists/${DISTRO_CODENAME}-pgdg/Release" &>/dev/null; then if ! curl -fsSL "https://apt.postgresql.org/pub/repos/apt/dists/${DISTRO_CODENAME}-pgdg/Release" &>/dev/null; then
case "$DISTRO_ID" in case "$DISTRO_ID" in
debian) debian) SUITE="bookworm" ;;
case "$DISTRO_CODENAME" in ubuntu) SUITE="jammy" ;;
trixie) SUITE="bookworm" ;;
*) SUITE="bookworm" ;;
esac
;;
ubuntu)
case "$DISTRO_CODENAME" in
oracular | noble) SUITE="jammy" ;;
*) SUITE="jammy" ;;
esac
;;
esac esac
msg_info "Using fallback suite: $SUITE (original: $DISTRO_CODENAME)"
fi fi
cat <<EOF >/etc/apt/sources.list.d/pgdg.sources cat <<EOF >/etc/apt/sources.list.d/pgdg.sources
@@ -1831,38 +1813,27 @@ Signed-By: /etc/apt/keyrings/postgresql.gpg
EOF EOF
$STD apt update $STD apt update
msg_ok "Repository added"
msg_info "Setup PostgreSQL $PG_VERSION"
$STD apt install -y "postgresql-${PG_VERSION}" "postgresql-client-${PG_VERSION}" $STD apt install -y "postgresql-${PG_VERSION}" "postgresql-client-${PG_VERSION}"
if [[ -n "$CURRENT_PG_VERSION" ]]; then if [[ -n "$CURRENT_PG_VERSION" ]]; then
$STD apt purge -y "postgresql-${CURRENT_PG_VERSION}" "postgresql-client-${CURRENT_PG_VERSION}" || true $STD apt purge -y "postgresql-${CURRENT_PG_VERSION}" "postgresql-client-${CURRENT_PG_VERSION}" || true
$STD su - postgres -c "psql < /var/lib/postgresql/backup_$(date +%F)_v${CURRENT_PG_VERSION}.sql"
fi fi
systemctl enable -q --now postgresql $STD systemctl enable --now postgresql
msg_ok "Installed PostgreSQL $PG_VERSION"
if [[ -n "$CURRENT_PG_VERSION" ]]; then
msg_info "Restoring dumped data"
su - postgres -c "psql < /var/lib/postgresql/backup_$(date +%F)_v${CURRENT_PG_VERSION}.sql"
msg_ok "Data restored"
fi
msg_ok "PostgreSQL $PG_VERSION installed"
fi fi
# Install optional PostgreSQL modules
if [[ -n "$PG_MODULES" ]]; then if [[ -n "$PG_MODULES" ]]; then
msg_info "Installing PostgreSQL modules"
IFS=',' read -ra MODULES <<<"$PG_MODULES" IFS=',' read -ra MODULES <<<"$PG_MODULES"
for module in "${MODULES[@]}"; do for module in "${MODULES[@]}"; do
local pkg="postgresql-${PG_VERSION}-${module}" $STD apt install -y "postgresql-${PG_VERSION}-${module}" || {
msg_info "Setup PostgreSQL module/s: $pkg" msg_warn "Failed to install postgresql-${PG_VERSION}-${module}"
$STD apt install -y "$pkg" || {
msg_error "Failed to install $pkg"
continue continue
} }
done done
msg_ok "Setup PostgreSQL modules" msg_ok "Installed PostgreSQL modules"
fi fi
} }