Skip to content

Instantly share code, notes, and snippets.

@bradgessler
Last active January 2, 2025 17:43
Show Gist options
  • Save bradgessler/dea60ee596b931328765e6ca95e8e97e to your computer and use it in GitHub Desktop.
Save bradgessler/dea60ee596b931328765e6ca95e8e97e to your computer and use it in GitHub Desktop.
When you don't have time to mess around worrying about why Postgres won't restart
#!/bin/bash
# Stop the PostgreSQL service
echo "Stopping PostgreSQL service..."
brew services stop postgresql || echo "PostgreSQL service not running."
# Uninstall PostgreSQL
echo "Uninstalling PostgreSQL..."
brew uninstall --force postgresql postgresql@14 postgresql@13 || echo "PostgreSQL not installed."
# Remove leftover data directories
echo "Removing PostgreSQL data directories..."
DATA_DIRS=(
"/opt/homebrew/var/postgresql"
"/opt/homebrew/var/postgresql@*"
"/usr/local/var/postgresql"
"/usr/local/var/postgresql@*"
)
for dir in "${DATA_DIRS[@]}"; do
if [ -d "$dir" ]; then
echo "Removing $dir..."
rm -rf "$dir"
fi
done
# Remove leftover configuration files
echo "Cleaning up configuration files..."
rm -rf ~/.psql_history ~/.pgpass
# Remove Homebrew cache for PostgreSQL
echo "Clearing Homebrew cache..."
brew cleanup --prune=all postgresql || echo "Cache already clean."
echo "Reinstalling PostgreSQL..."
brew install postgresql
echo "Starting PostgreSQL service..."
brew services start postgresql
echo "Reinitializing the database..."
initdb "$(brew --prefix)/var/postgresql"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment