DockerPostgresMigration/migrate.sh

38 lines
1.0 KiB
Bash
Raw Permalink Normal View History

2024-12-06 10:32:50 +00:00
#!/bin/bash
echo Migrate PostgreSQL version
DB_PATH=/db
OLD_DB_PATH=/tmp/old_db
2024-12-06 10:55:41 +00:00
NEW_DB_PATH=/tmp/new_db
2024-12-06 10:32:50 +00:00
2024-12-06 10:55:41 +00:00
# Handle password
if [ -z "$POSTGRES_PASSWORD" ] || [ "$POSTGRES_PASSWORD" == " " ];
then
echo "Please specify database password in POSTGRES_PASSWORD env variable!"
exit 1;
fi
2024-12-06 10:32:50 +00:00
2024-12-06 10:55:41 +00:00
# Run database on a COPY of the database
cp -r "$DB_PATH" "$OLD_DB_PATH"
2024-12-06 10:32:50 +00:00
2024-12-06 10:55:41 +00:00
# Initialize NEW database
mkdir "$NEW_DB_PATH"
2024-12-06 12:10:24 +00:00
/postgres/17/bin/initdb -D "$NEW_DB_PATH" --username "$POSTGRES_USER" --pwfile=<(printf "%s\n" "$POSTGRES_PASSWORD") || exit 1
2024-12-06 10:32:50 +00:00
2024-12-06 10:55:41 +00:00
# Perform migration
cd /tmp
/postgres/17/bin/pg_upgrade -b /postgres/16/bin/ -d "$OLD_DB_PATH" -D "$NEW_DB_PATH" -U "$POSTGRES_USER" || exit 2
2024-12-06 10:32:50 +00:00
2024-12-06 13:17:36 +00:00
# Configure database server
echo Configure database server
cp "$OLD_DB_PATH/pg_hba.conf" "$NEW_DB_PATH/pg_hba.conf"
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" "$NEW_DB_PATH/postgresql.conf"
# Replace database
echo "Replace database files..."
rm -rf "$DB_PATH"/*
cp -r "$NEW_DB_PATH"/* "$DB_PATH"
mv "$OLD_DB_PATH" "$DB_PATH"
echo "Done with migration."