4 Commits

Author SHA1 Message Date
1c061ebbee Ensure the generated image include PG contrib extensions 2025-10-09 09:34:47 +02:00
b9458ced17 Fix issue with checksums 2025-10-08 20:41:40 +02:00
31fdceb34f Prepare migration to 17 -> 18 2025-10-08 19:39:26 +02:00
677b1d980b Add README 2024-12-07 23:09:37 +01:00
4 changed files with 41 additions and 21 deletions

View File

@@ -1,18 +1,16 @@
FROM ubuntu:noble AS build18
RUN apt update && apt upgrade -y && apt install -y wget curl tar build-essential libreadline-dev zlib1g-dev flex bison libicu-dev pkg-config
RUN wget https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0.tar.gz -O pg18.tar.gz
RUN tar -xf pg18.tar.gz
RUN cd postgresql* && ./configure --prefix=/postgres/18 && make world-bin && make install-world-bin
FROM ubuntu:noble AS build17 FROM ubuntu:noble AS build17
RUN apt update && apt upgrade -y && apt install -y wget curl tar build-essential libreadline-dev zlib1g-dev flex bison libicu-dev pkg-config RUN apt update && apt upgrade -y && apt install -y wget curl tar build-essential libreadline-dev zlib1g-dev flex bison libicu-dev pkg-config
RUN wget https://ftp.postgresql.org/pub/source/v17.6/postgresql-17.6.tar.gz -O pg17.tar.gz
RUN wget https://ftp.postgresql.org/pub/source/v17.2/postgresql-17.2.tar.gz -O pg17.tar.gz
RUN tar -xf pg17.tar.gz RUN tar -xf pg17.tar.gz
RUN cd postgresql* && ./configure --prefix=/postgres/17 && make world-bin && make install RUN cd postgresql* && ./configure --prefix=/postgres/17 && make world-bin && make install-world-bin
FROM ubuntu:noble AS build16
RUN apt update && apt upgrade -y && apt install -y wget curl tar build-essential libreadline-dev zlib1g-dev flex bison libicu-dev pkg-config
RUN wget https://ftp.postgresql.org/pub/source/v16.6/postgresql-16.6.tar.gz -O pg16.tar.gz
RUN tar -xf pg16.tar.gz
RUN cd postgresql* && ./configure --prefix=/postgres/16 && make world-bin && make install
FROM ubuntu:noble FROM ubuntu:noble
RUN apt update && apt install -y \ RUN apt update && apt install -y \
libicu74 \ libicu74 \
@@ -27,8 +25,9 @@ ENV LANG=en_US.utf8
ENV POSTGRES_USER=postgres ENV POSTGRES_USER=postgres
COPY --from=build18 /postgres/18 /postgres/18
COPY --from=build17 /postgres/17 /postgres/17 COPY --from=build17 /postgres/17 /postgres/17
COPY --from=build16 /postgres/16 /postgres/16
COPY migrate.sh /migrate.sh COPY migrate.sh /migrate.sh
RUN chmod +x migrate.sh RUN chmod +x migrate.sh

View File

@@ -1,13 +1,22 @@
# Docker migration helper # Docker migration helper
This repo contains a `Dockerfile` that can helps you in performing your Docker images migration.
## Test This repository will:
Create an outdated database
* Perform a temporary copy the old database
* Initialize a new empty database with the default user / password couple provided as environment variables (`POSTGRES_USER` and `POSTGRES_PASSWORD`)
* Run `pg_upgrade` from the copy of the old database to the new database
* Replace the old database with the new one
* Move the copy of the old database as sub-directory of the new database (in `old_db` directory, to allow rollback in case of failure)
## Perform the migration
Create an outdated database:
```bash ```bash
mkdir test-db mkdir test-db
docker run --rm --name testdb -u 1000 -e POSTGRES_PASSWORD=password -v $(pwd)/test-db:/var/lib/postgresql/data -it postgres:16 docker run --rm --name testdb --env PGDATA=/var/lib/postgresql/data -u 1000 -e POSTGRES_PASSWORD=password -v $(pwd)/test-db:/var/lib/postgresql/data -it postgres:17
``` ```
Perform the migration: Perform the migration (you need to replace `POSTGRES_USER` and `POSTGRES_PASSWORD` with the credentials of the root user of the database):
```bash ```bash
./build.sh && docker run -u 1000 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -v $(pwd)/test-db:/db --rm -it pierre42100/postgresmig:16to17 docker run -u 1000 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -v $(pwd)/test-db:/db --rm -it pierre42100/postgresmig:17to18
``` ```

View File

@@ -1,2 +1,2 @@
#!/bin/bash #!/bin/bash
docker build . -t pierre42100/postgresmig:16to17 docker build . -t pierre42100/postgresmig:17to18

View File

@@ -1,6 +1,9 @@
#!/bin/bash #!/bin/bash
echo Migrate PostgreSQL version echo Migrate PostgreSQL version
OLD_POSTGRES=/postgres/17
NEW_POSTGRES=/postgres/18
DB_PATH=/db DB_PATH=/db
OLD_DB_PATH=/tmp/old_db OLD_DB_PATH=/tmp/old_db
NEW_DB_PATH=/tmp/new_db NEW_DB_PATH=/tmp/new_db
@@ -12,17 +15,26 @@ then
exit 1; exit 1;
fi fi
# Run database on a COPY of the database # Run migration on a COPY of the database
cp -r "$DB_PATH" "$OLD_DB_PATH" cp -r "$DB_PATH" "$OLD_DB_PATH"
# Initialize NEW database # Initialize NEW database
mkdir "$NEW_DB_PATH" mkdir "$NEW_DB_PATH"
/postgres/17/bin/initdb -D "$NEW_DB_PATH" --username "$POSTGRES_USER" --pwfile=<(printf "%s\n" "$POSTGRES_PASSWORD") || exit 1 $NEW_POSTGRES/bin/initdb -D "$NEW_DB_PATH" --username "$POSTGRES_USER" --pwfile=<(printf "%s\n" "$POSTGRES_PASSWORD") || exit 1
# Enable / disable checksum on new database, depending on the configuration of the old one
if $OLD_POSTGRES/bin/pg_checksums -c "$OLD_DB_PATH";
then
echo Enable checksums on new database
$NEW_POSTGRES/bin/pg_checksums -e "$NEW_DB_PATH"
else
echo Disable checksums on new database
$NEW_POSTGRES/bin/pg_checksums -d "$NEW_DB_PATH"
fi
# Perform migration # Perform migration
cd /tmp cd /tmp
/postgres/17/bin/pg_upgrade -b /postgres/16/bin/ -d "$OLD_DB_PATH" -D "$NEW_DB_PATH" -U "$POSTGRES_USER" || exit 2 $NEW_POSTGRES/bin/pg_upgrade -b $OLD_POSTGRES/bin/ -d "$OLD_DB_PATH" -D "$NEW_DB_PATH" -U "$POSTGRES_USER" || exit 2
# Configure database server # Configure database server
echo Configure database server echo Configure database server