Skip to content

Commit 99d441e

Browse files
committed
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.
1 parent 55c2141 commit 99d441e

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

misc/tools.func

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3805,10 +3805,73 @@ function setup_postgresql() {
38053805
local SUITE
38063806
case "$DISTRO_CODENAME" in
38073807
trixie | forky | sid)
3808+
# For Debian Testing/Unstable, try PostgreSQL repo first, fallback to native packages
38083809
if verify_repo_available "https://apt.postgresql.org/pub/repos/apt" "trixie-pgdg"; then
38093810
SUITE="trixie-pgdg"
3811+
3812+
setup_deb822_repo \
3813+
"pgdg" \
3814+
"https://www.postgresql.org/media/keys/ACCC4CF8.asc" \
3815+
"https://apt.postgresql.org/pub/repos/apt" \
3816+
"$SUITE" \
3817+
"main"
3818+
3819+
if ! $STD apt update; then
3820+
msg_warn "Failed to update PostgreSQL repository, falling back to native packages"
3821+
SUITE=""
3822+
fi
38103823
else
3811-
SUITE="bookworm-pgdg"
3824+
SUITE=""
3825+
fi
3826+
3827+
# If no repo or packages not installable, use native Debian packages
3828+
if [[ -z "$SUITE" ]] || ! apt-cache show "postgresql-${PG_VERSION}" 2>/dev/null | grep -q "Version:"; then
3829+
msg_info "Using native Debian packages for $DISTRO_CODENAME"
3830+
3831+
# Install ssl-cert dependency if available
3832+
if apt-cache search "^ssl-cert$" 2>/dev/null | grep -q .; then
3833+
$STD apt install -y ssl-cert 2>/dev/null || true
3834+
fi
3835+
3836+
if ! $STD apt install -y postgresql postgresql-client 2>/dev/null; then
3837+
msg_error "Failed to install native PostgreSQL packages"
3838+
return 1
3839+
fi
3840+
3841+
if ! command -v psql >/dev/null 2>&1; then
3842+
msg_error "PostgreSQL installed but psql command not found"
3843+
return 1
3844+
fi
3845+
3846+
# Restore database backup if we upgraded from previous version
3847+
if [[ -n "$CURRENT_PG_VERSION" ]]; then
3848+
msg_info "Restoring PostgreSQL databases from backup..."
3849+
$STD runuser -u postgres -- psql </var/lib/postgresql/backup_$(date +%F)_v${CURRENT_PG_VERSION}.sql 2>/dev/null || {
3850+
msg_warn "Failed to restore database backup - this may be expected for major version upgrades"
3851+
}
3852+
fi
3853+
3854+
$STD systemctl enable --now postgresql 2>/dev/null || true
3855+
3856+
# Get actual installed version
3857+
INSTALLED_VERSION="$(psql -V 2>/dev/null | awk '{print $3}' | cut -d. -f1)"
3858+
3859+
# Add PostgreSQL binaries to PATH
3860+
if ! grep -q '/usr/lib/postgresql' /etc/environment 2>/dev/null; then
3861+
echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/'"${INSTALLED_VERSION}"'/bin"' >/etc/environment
3862+
fi
3863+
3864+
cache_installed_version "postgresql" "$INSTALLED_VERSION"
3865+
msg_ok "Setup PostgreSQL $INSTALLED_VERSION (native)"
3866+
3867+
# Install optional modules if specified
3868+
if [[ -n "$PG_MODULES" ]]; then
3869+
IFS=',' read -ra MODULES <<<"$PG_MODULES"
3870+
for module in "${MODULES[@]}"; do
3871+
$STD apt install -y "postgresql-${INSTALLED_VERSION}-${module}" 2>/dev/null || true
3872+
done
3873+
fi
3874+
return 0
38123875
fi
38133876
;;
38143877
*)

0 commit comments

Comments
 (0)