From e20fed1a2dd8358b897443c33f26cd6d49f794e3 Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Wed, 18 Mar 2026 16:45:11 +0100 Subject: [PATCH] tools.func Implement pg_cron setup for setup_postgresql (#13053) * tools.func Implement PostgreSQL setup and upgrade function Added setup_postgresql function to install or upgrade PostgreSQL, including optional modules and backup restoration. * correct diff * Update tools.func * Update tools.func * Update tools.func * Update tools.func --- misc/tools.func | 50 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/misc/tools.func b/misc/tools.func index cfb004788..799d03c96 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -6698,22 +6698,38 @@ EOF # - Optionally uses official PGDG repository for specific versions # - Detects existing PostgreSQL version # - Dumps all databases before upgrade -# - Installs optional PG_MODULES (e.g. postgis, contrib) +# - Installs optional PG_MODULES (e.g. postgis, contrib, cron) # - Restores dumped data post-upgrade # # Variables: # USE_PGDG_REPO - Use official PGDG repository (default: true) # Set to "false" to use distro packages instead # PG_VERSION - Major PostgreSQL version (e.g. 15, 16) (default: 16) -# PG_MODULES - Comma-separated list of modules (e.g. "postgis,contrib") +# PG_MODULES - Comma-separated list of modules (e.g. "postgis,contrib,cron") # # Examples: -# setup_postgresql # Uses PGDG repo, PG 16 -# PG_VERSION="17" setup_postgresql # Specific version from PGDG -# USE_PGDG_REPO=false setup_postgresql # Uses distro package instead +# setup_postgresql # Uses PGDG repo, PG 16 +# PG_VERSION="17" setup_postgresql # Specific version from PGDG +# USE_PGDG_REPO=false setup_postgresql # Uses distro package instead +# PG_VERSION="17" PG_MODULES="cron" setup_postgresql # With pg_cron module # ------------------------------------------------------------------------------ -function setup_postgresql() { +# Internal helper: Configure shared_preload_libraries for pg_cron +_configure_pg_cron_preload() { + local modules="${1:-}" + [[ -z "$modules" ]] && return 0 + if [[ ",$modules," == *",cron,"* ]]; then + local current_libs + current_libs=$(sudo -u postgres psql -tAc "SHOW shared_preload_libraries;" 2>/dev/null || echo "") + if [[ "$current_libs" != *"pg_cron"* ]]; then + local new_libs="${current_libs:+${current_libs},}pg_cron" + $STD sudo -u postgres psql -c "ALTER SYSTEM SET shared_preload_libraries = '${new_libs}';" + $STD systemctl restart postgresql + fi + fi +} + +setup_postgresql() { local PG_VERSION="${PG_VERSION:-16}" local PG_MODULES="${PG_MODULES:-}" local USE_PGDG_REPO="${USE_PGDG_REPO:-true}" @@ -6751,6 +6767,7 @@ function setup_postgresql() { $STD apt install -y "postgresql-${CURRENT_PG_VERSION}-${module}" 2>/dev/null || true done fi + _configure_pg_cron_preload "$PG_MODULES" return 0 fi @@ -6786,6 +6803,7 @@ function setup_postgresql() { $STD apt install -y "postgresql-${INSTALLED_VERSION}-${module}" 2>/dev/null || true done fi + _configure_pg_cron_preload "$PG_MODULES" return 0 fi @@ -6807,6 +6825,7 @@ function setup_postgresql() { $STD apt install -y "postgresql-${PG_VERSION}-${module}" 2>/dev/null || true done fi + _configure_pg_cron_preload "$PG_MODULES" return 0 fi @@ -6834,13 +6853,16 @@ function setup_postgresql() { local SUITE case "$DISTRO_CODENAME" in trixie | forky | sid) + if verify_repo_available "https://apt.postgresql.org/pub/repos/apt" "trixie-pgdg"; then SUITE="trixie-pgdg" + else msg_warn "PGDG repo not available for ${DISTRO_CODENAME}, falling back to distro packages" USE_PGDG_REPO=false setup_postgresql return $? fi + ;; *) SUITE=$(get_fallback_suite "$DISTRO_ID" "$DISTRO_CODENAME" "https://apt.postgresql.org/pub/repos/apt") @@ -6924,6 +6946,7 @@ function setup_postgresql() { } done fi + _configure_pg_cron_preload "$PG_MODULES" } # ------------------------------------------------------------------------------ @@ -6942,6 +6965,7 @@ function setup_postgresql() { # PG_DB_NAME="immich" PG_DB_USER="immich" PG_DB_EXTENSIONS="pgvector" setup_postgresql_db # PG_DB_NAME="ghostfolio" PG_DB_USER="ghostfolio" PG_DB_GRANT_SUPERUSER="true" setup_postgresql_db # PG_DB_NAME="adventurelog" PG_DB_USER="adventurelog" PG_DB_EXTENSIONS="postgis" setup_postgresql_db +# PG_DB_NAME="splitpro" PG_DB_USER="splitpro" PG_DB_EXTENSIONS="pg_cron" setup_postgresql_db # # Variables: # PG_DB_NAME - Database name (required) @@ -6973,6 +6997,13 @@ function setup_postgresql_db() { $STD sudo -u postgres psql -c "CREATE ROLE $PG_DB_USER WITH LOGIN PASSWORD '$PG_DB_PASS';" $STD sudo -u postgres psql -c "CREATE DATABASE $PG_DB_NAME WITH OWNER $PG_DB_USER ENCODING 'UTF8' TEMPLATE template0;" + # Configure pg_cron database BEFORE creating the extension (must be set before pg_cron loads) + if [[ -n "${PG_DB_EXTENSIONS:-}" ]] && [[ ",${PG_DB_EXTENSIONS//[[:space:]]/}," == *",pg_cron,"* ]]; then + $STD sudo -u postgres psql -c "ALTER SYSTEM SET cron.database_name = '${PG_DB_NAME}';" + $STD sudo -u postgres psql -c "ALTER SYSTEM SET cron.timezone = 'UTC';" + $STD systemctl restart postgresql + fi + # Install extensions (comma-separated) if [[ -n "${PG_DB_EXTENSIONS:-}" ]]; then IFS=',' read -ra EXT_LIST <<<"${PG_DB_EXTENSIONS:-}" @@ -6982,6 +7013,12 @@ function setup_postgresql_db() { done fi + # Grant pg_cron schema permissions to DB user + if [[ -n "${PG_DB_EXTENSIONS:-}" ]] && [[ ",${PG_DB_EXTENSIONS//[[:space:]]/}," == *",pg_cron,"* ]]; then + $STD sudo -u postgres psql -d "$PG_DB_NAME" -c "GRANT USAGE ON SCHEMA cron TO ${PG_DB_USER};" + $STD sudo -u postgres psql -d "$PG_DB_NAME" -c "GRANT ALL ON ALL TABLES IN SCHEMA cron TO ${PG_DB_USER};" + fi + # ALTER ROLE settings for Django/Rails compatibility (unless skipped) if [[ "${PG_DB_SKIP_ALTER_ROLE:-}" != "true" ]]; then $STD sudo -u postgres psql -c "ALTER ROLE $PG_DB_USER SET client_encoding TO 'utf8';" @@ -7023,7 +7060,6 @@ function setup_postgresql_db() { export PG_DB_USER export PG_DB_PASS } - # ------------------------------------------------------------------------------ # Installs rbenv and ruby-build, installs Ruby and optionally Rails. #