From 604b7818beeeefbdd2f67640024974bfebf0f9ad Mon Sep 17 00:00:00 2001 From: Roman Vanicek Date: Sun, 29 Mar 2026 00:23:52 +0000 Subject: [PATCH] Automatic upgrade --- Dockerfile | 9 +++++++-- upgrade-entrypoint.sh | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 upgrade-entrypoint.sh diff --git a/Dockerfile b/Dockerfile index e52f3c4..7aa5561 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,9 +11,14 @@ FROM postgis/postgis:16-master COPY --from=builder /usr/local/src/pg_autoconfig/bin/pg_autoconfig / +COPY upgrade-entrypoint.sh / + RUN localedef -i cs_CZ -c -f UTF-8 -A /usr/share/locale/locale.alias cz_CZ.UTF-8 && \ - apt-get update && apt-get install -y postgresql-16-auto-failover pg-auto-failover-cli postgresql-16-pgvector && rm -rf /var/lib/apt/lists/* + apt-get update && apt-get install -y postgresql-16-auto-failover pg-auto-failover-cli postgresql-16-pgvector && \ + rm -rf /var/lib/apt/lists/* && \ + chmod +x /upgrade-entrypoint.sh #ENV LANG en_GB.utf8 -ENTRYPOINT [ "/pg_autoconfig" ] \ No newline at end of file +ENTRYPOINT [ "/upgrade-entrypoint.sh" ] +CMD [ "/pg_autoconfig" ] \ No newline at end of file diff --git a/upgrade-entrypoint.sh b/upgrade-entrypoint.sh new file mode 100644 index 0000000..64ecb2d --- /dev/null +++ b/upgrade-entrypoint.sh @@ -0,0 +1,38 @@ +#!/bin/bash +set -e + +DATA_DIR="/var/lib/postgresql/data" +OLD_VERSION_FILE="$DATA_DIR/PG_VERSION" +NEW_VERSION="16" + +if [ -f "$OLD_VERSION_FILE" ]; then + OLD_VERSION=$(cat "$OLD_VERSION_FILE") + + if [ "$OLD_VERSION" != "$NEW_VERSION" ]; then + echo "Data version ($OLD_VERSION) differs from engine ($NEW_VERSION). Starting upgrade..." + + # 1. Move old data to a temporary folder + mkdir -p $DATA_DIR/old_data + mv $DATA_DIR/* $DATA_DIR/old_data/ || true + + # 2. Initialize new data directory + mkdir $DATA_DIR/new_data + initdb -D $DATA_DIR/new_data + + # 3. Run pg_upgrade + # --link is used to avoid copying files (fast, but requires a backup!) + pg_upgrade \ + -d $DATA_DIR/old_data \ + -D $DATA_DIR/new_data \ + -b /usr/bin/postgres-16 \ + -B /usr/local/bin \ + --link + + echo "Upgrade complete. Cleaning up..." + rm -rf $DATA_DIR/old_data + mv $DATA_DIR/new_data/* $DATA_DIR/ + rmdir $DATA_DIR/new_data + fi +fi + +exec $@ \ No newline at end of file