From 99d441e37c7ad3afc6ee35076856ca1528198c7d Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Mon, 17 Nov 2025 09:42:02 +0100 Subject: [PATCH] Improve PostgreSQL setup for Debian testing/unstable Enhances the setup_postgresql function to prioritize the PostgreSQL upstream repository for Debian trixie, forky, and sid, with a fallback to native Debian packages if the repo is unavailable or packages cannot be installed. Adds logic for installing dependencies, restoring backups, enabling the service, updating PATH, and installing optional modules for native package installations. --- misc/tools.func | 65 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/misc/tools.func b/misc/tools.func index d87289d58..975d4653b 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -3805,10 +3805,73 @@ function setup_postgresql() { local SUITE case "$DISTRO_CODENAME" in trixie | forky | sid) + # For Debian Testing/Unstable, try PostgreSQL repo first, fallback to native packages if verify_repo_available "https://apt.postgresql.org/pub/repos/apt" "trixie-pgdg"; then SUITE="trixie-pgdg" + + setup_deb822_repo \ + "pgdg" \ + "https://www.postgresql.org/media/keys/ACCC4CF8.asc" \ + "https://apt.postgresql.org/pub/repos/apt" \ + "$SUITE" \ + "main" + + if ! $STD apt update; then + msg_warn "Failed to update PostgreSQL repository, falling back to native packages" + SUITE="" + fi else - SUITE="bookworm-pgdg" + SUITE="" + fi + + # If no repo or packages not installable, use native Debian packages + if [[ -z "$SUITE" ]] || ! apt-cache show "postgresql-${PG_VERSION}" 2>/dev/null | grep -q "Version:"; then + msg_info "Using native Debian packages for $DISTRO_CODENAME" + + # Install ssl-cert dependency if available + if apt-cache search "^ssl-cert$" 2>/dev/null | grep -q .; then + $STD apt install -y ssl-cert 2>/dev/null || true + fi + + if ! $STD apt install -y postgresql postgresql-client 2>/dev/null; then + msg_error "Failed to install native PostgreSQL packages" + return 1 + fi + + if ! command -v psql >/dev/null 2>&1; then + msg_error "PostgreSQL installed but psql command not found" + return 1 + fi + + # Restore database backup if we upgraded from previous version + if [[ -n "$CURRENT_PG_VERSION" ]]; then + msg_info "Restoring PostgreSQL databases from backup..." + $STD runuser -u postgres -- psql /dev/null || { + msg_warn "Failed to restore database backup - this may be expected for major version upgrades" + } + fi + + $STD systemctl enable --now postgresql 2>/dev/null || true + + # Get actual installed version + INSTALLED_VERSION="$(psql -V 2>/dev/null | awk '{print $3}' | cut -d. -f1)" + + # Add PostgreSQL binaries to PATH + if ! grep -q '/usr/lib/postgresql' /etc/environment 2>/dev/null; then + echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/'"${INSTALLED_VERSION}"'/bin"' >/etc/environment + fi + + cache_installed_version "postgresql" "$INSTALLED_VERSION" + msg_ok "Setup PostgreSQL $INSTALLED_VERSION (native)" + + # Install optional modules if specified + if [[ -n "$PG_MODULES" ]]; then + IFS=',' read -ra MODULES <<<"$PG_MODULES" + for module in "${MODULES[@]}"; do + $STD apt install -y "postgresql-${INSTALLED_VERSION}-${module}" 2>/dev/null || true + done + fi + return 0 fi ;; *)