Created
January 26, 2024 16:35
-
-
Save defaye/b3f2d4faa6b52838c1edf965e8067808 to your computer and use it in GitHub Desktop.
Swap Postgres version via Homebrew
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
swap_pg_version() { | |
if [ $# -ne 1 ]; then | |
echo "Usage: swap_pg_version <version>" | |
return 1 | |
fi | |
TARGET_VERSION="postgresql@$1" | |
# List all installed PostgreSQL versions | |
INSTALLED_VERSIONS=$(brew list --formula | grep 'postgresql@') | |
# Find the currently running version | |
RUNNING_VERSION=$(brew services list | grep 'postgresql@' | grep 'started' | awk '{print $1}') | |
# Check if the target version is installed | |
if echo "$INSTALLED_VERSIONS" | grep -q "^$TARGET_VERSION\$"; then | |
# Stop the currently running version if it's different from the target version | |
if [ -n "$RUNNING_VERSION" ] && [ "$RUNNING_VERSION" != "$TARGET_VERSION" ]; then | |
echo "Stopping the currently running $RUNNING_VERSION..." | |
brew services stop $RUNNING_VERSION | |
fi | |
# Unlink the current version | |
if [ -n "$RUNNING_VERSION" ]; then | |
brew unlink $RUNNING_VERSION | |
fi | |
# Link the target version | |
brew link $TARGET_VERSION --force --overwrite | |
# Start the new version | |
brew services start $TARGET_VERSION | |
echo "Switched to $TARGET_VERSION" | |
else | |
echo "PostgreSQL version $1 is not installed." | |
return 1 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment