Last active
November 27, 2025 06:26
-
-
Save bouroo/1532bfcc637139b85b269b10692a41a3 to your computer and use it in GitHub Desktop.
pgauto docker script to update postgres database data version
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # PostgreSQL Auto Upgrade Script with Version and OS Selection | |
| # Usage: ./upgrade_pg.sh [version] [os] [mount_type] | |
| # Default values | |
| DEFAULT_VERSION="18" | |
| DEFAULT_OS="alpine" | |
| DEFAULT_MOUNT_TYPE="bind" | |
| # Available options | |
| VERSIONS=("15" "16" "17" "18") | |
| OSS=("alpine" "debian") | |
| MOUNT_TYPES=("bind" "volume") | |
| # Function to display usage | |
| usage() { | |
| echo "Usage: $0 [version] [os] [mount_type]" | |
| echo "" | |
| echo "Available versions: ${VERSIONS[*]}" | |
| echo "Available OS options: ${OSS[*]}" | |
| echo "Available mount types: ${MOUNT_TYPES[*]}" | |
| echo "" | |
| echo "Defaults:" | |
| echo " Version: $DEFAULT_VERSION" | |
| echo " OS: $DEFAULT_OS" | |
| echo " Mount type: $DEFAULT_MOUNT_TYPE" | |
| echo "" | |
| echo "Examples:" | |
| echo " $0 # Uses defaults (18-alpine-bind)" | |
| echo " $0 16 # Uses version 16 with default OS and mount type (16-alpine-bind)" | |
| echo " $0 18 debian # Uses version 18 with debian OS and default mount type (18-debian-bind)" | |
| echo " $0 15 alpine volume # Uses version 15 with alpine OS and volume mount (15-alpine-volume)" | |
| echo " $0 17 debian bind # Uses version 17 with debian OS and bind mount (17-debian-bind)" | |
| exit 1 | |
| } | |
| # Function to validate version | |
| validate_version() { | |
| local version=$1 | |
| for v in "${VERSIONS[@]}"; do | |
| if [[ "$v" == "$version" ]]; then | |
| return 0 | |
| fi | |
| done | |
| return 1 | |
| } | |
| # Function to validate OS | |
| validate_os() { | |
| local os=$1 | |
| for o in "${OSS[@]}"; do | |
| if [[ "$o" == "$os" ]]; then | |
| return 0 | |
| fi | |
| done | |
| return 1 | |
| } | |
| # Function to validate mount type | |
| validate_mount_type() { | |
| local mount_type=$1 | |
| for mt in "${MOUNT_TYPES[@]}"; do | |
| if [[ "$mt" == "$mount_type" ]]; then | |
| return 0 | |
| fi | |
| done | |
| return 1 | |
| } | |
| # Parse command line arguments | |
| VERSION=$DEFAULT_VERSION | |
| OS=$DEFAULT_OS | |
| MOUNT_TYPE=$DEFAULT_MOUNT_TYPE | |
| # Check number of arguments | |
| if [[ $# -gt 3 ]]; then | |
| usage | |
| fi | |
| # Parse arguments with defaults | |
| if [[ $# -ge 1 ]]; then | |
| VERSION=$1 | |
| if ! validate_version "$VERSION"; then | |
| echo "Error: Invalid version '$VERSION'" | |
| echo "Available versions: ${VERSIONS[*]}" | |
| exit 1 | |
| fi | |
| fi | |
| if [[ $# -ge 2 ]]; then | |
| OS=$2 | |
| if ! validate_os "$OS"; then | |
| echo "Error: Invalid OS '$OS'" | |
| echo "Available OS options: ${OSS[*]}" | |
| exit 1 | |
| fi | |
| fi | |
| if [[ $# -eq 3 ]]; then | |
| MOUNT_TYPE=$3 | |
| if ! validate_mount_type "$MOUNT_TYPE"; then | |
| echo "Error: Invalid mount type '$MOUNT_TYPE'" | |
| echo "Available mount types: ${MOUNT_TYPES[*]}" | |
| exit 1 | |
| fi | |
| fi | |
| # Set the Docker image tag | |
| DOCKER_IMAGE="pgautoupgrade/pgautoupgrade:${VERSION}-${OS}" | |
| echo "Starting PostgreSQL upgrade..." | |
| echo "Target version: $VERSION" | |
| echo "Target OS: $OS" | |
| echo "Mount type: $MOUNT_TYPE" | |
| echo "Docker image: $DOCKER_IMAGE" | |
| echo "" | |
| # Check if required environment variables are set | |
| if [[ -z "$PGDATA" ]]; then | |
| echo "Error: PGDATA environment variable is not set" | |
| echo "Please set PGDATA to your PostgreSQL data directory" | |
| exit 1 | |
| fi | |
| if [[ -z "$POSTGRES_USER" ]]; then | |
| echo "Error: POSTGRES_USER environment variable is not set" | |
| echo "Please set POSTGRES_USER to your PostgreSQL username" | |
| exit 1 | |
| fi | |
| if [[ -z "$POSTGRES_PASSWORD" ]]; then | |
| echo "Error: POSTGRES_PASSWORD environment variable is not set" | |
| echo "Please set POSTGRES_PASSWORD to your PostgreSQL password" | |
| exit 1 | |
| fi | |
| # Build the mount command based on mount type | |
| if [[ "$MOUNT_TYPE" == "bind" ]]; then | |
| # Use bind mount | |
| MOUNT_CMD="--mount type=bind,source=\"${PGDATA}\",target=/var/lib/postgresql/data" | |
| elif [[ "$MOUNT_TYPE" == "volume" ]]; then | |
| # Use volume mount | |
| # Extract volume name from PGDATA path or use default | |
| if [[ "$PGDATA" == /* ]]; then | |
| # For absolute paths, use the directory name as volume name | |
| VOLUME_NAME=$(basename "$PGDATA") | |
| else | |
| # For relative paths, use the full path as volume name | |
| VOLUME_NAME="${PGDATA}" | |
| fi | |
| MOUNT_CMD="--mount type=volume,source=\"${VOLUME_NAME}\",target=/var/lib/postgresql/data" | |
| else | |
| echo "Error: Invalid mount type '$MOUNT_TYPE'" | |
| exit 1 | |
| fi | |
| # Run the upgrade container | |
| docker run --name pgauto -it \ | |
| ${MOUNT_CMD} \ | |
| -e TZ="Asia/Bangkok" \ | |
| -e LANG="C.UTF-8" \ | |
| -e POSTGRES_USER="${POSTGRES_USER}" \ | |
| -e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \ | |
| -e PGAUTO_ONESHOT=yes \ | |
| "$DOCKER_IMAGE" | |
| # Cleanup | |
| if [[ $? -eq 0 ]]; then | |
| echo "" | |
| echo "Upgrade completed successfully!" | |
| echo "Cleaning up container..." | |
| docker rm pgauto | |
| else | |
| echo "" | |
| echo "Upgrade failed!" | |
| echo "Container 'pgauto' may still exist for debugging" | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment