-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change: Add upgrade scripts for database files
Postgres changes the format of the database files on disk between major versions. This means that in order to upgrade a postgres database, it is not sufficient to just bump the container versions. To help with this process, we now can use migration scripts that will take care of updating the database version without the need of a manual dump/restore. Ref.: DEVOPS-1292
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,28 @@ | ||
ARG POSTGRES_VERSION=16 | ||
# This image was forked from the official PostgreSQL image, so it could be | ||
# signed and relied on as an internal dependency. | ||
# The image is modified to include an older version of PostgreSQL, which | ||
# allows us to upgrade the database from the old version to the new one. | ||
ARG POSTGRES_VERSION=17 | ||
FROM postgres:${POSTGRES_VERSION} | ||
|
||
# we need to redeclare the ARG here, otherwise it will not | ||
# be available in the section below the FROM statement. | ||
ARG POSTGRES_VERSION=17 | ||
|
||
# Enable and install old version of PostgreSQL. | ||
RUN sed -i 's/$/ 16/' /etc/apt/sources.list.d/pgdg.list | ||
RUN set -eux; \ | ||
apt-get update; \ | ||
apt-get install -y --no-install-recommends \ | ||
postgresql-16 \ | ||
; \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
# The old binaries will be in /usr/lib/postgresql/16/bin | ||
ENV PGBINOLD /usr/lib/postgresql/16/bin | ||
ENV PGBINNEW /usr/lib/postgresql/17/bin | ||
|
||
ENV PGDATAOLD /var/lib/postgresql/16/data | ||
ENV PGDATANEW /var/lib/postgresql/17/data | ||
|
||
COPY bin/upgradeversion /usr/local/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
# Check postgres version | ||
echo -n "Current database version is " | ||
cat ${PGDATAOLD}/PG_VERSION | ||
|
||
# Abort if the old version is not 16 | ||
if [ "$(cat ${PGDATAOLD}/PG_VERSION)" != "16" ]; then | ||
echo "Only PostgreSQL 16 is supported for migration with this image." | ||
exit 1 | ||
fi | ||
|
||
chown -R postgres:postgres "${PGDATAOLD}" "${PGDATANEW}" | ||
|
||
# Initialize the new database, if it doesn't exist | ||
if [ ! -s "${PGDATANEW}/PG_VERSION" ]; then | ||
gosu postgres initdb -D ${PGDATANEW} | ||
fi | ||
|
||
ls -alh /var/lib/postgresql/17/data/ | ||
|
||
# mitigate: 'you must have read and write access in the current directory' | ||
cd /var/lib/postgresql | ||
|
||
gosu postgres pg_upgrade \ | ||
--old-bindir ${PGBINOLD} \ | ||
--new-bindir ${PGBINNEW} \ | ||
--old-datadir ${PGDATAOLD} \ | ||
--new-datadir ${PGDATANEW} | ||
|
||
# Check postgres version | ||
echo -n "New migrated database version is " | ||
cat ${PGDATANEW}/PG_VERSION | ||
|
||
# if first parameter is 'inplace', we copy the new postgres version to | ||
# the old one. This is not an atomic operation, and therefore risky; | ||
# If the copy operation is not successful, we will end up with a | ||
# corrupted database. | ||
if [ "$1" == "inplace" ]; then | ||
rm -rf ${PGDATAOLD}/* | ||
mv ${PGDATANEW} ${PGDATAOLD} | ||
fi |