Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenvac/52d952b73f4cc048abaa9da860966c94 to your computer and use it in GitHub Desktop.
Save kenvac/52d952b73f4cc048abaa9da860966c94 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Vacuum, analyze and re-index all non-template postgres databases
# Should be run as root to avoid any permissions issues
db_list_sql="SELECT datname FROM pg_database WHERE NOT datistemplate AND datallowconn ORDER BY datname"
for db in $(su - postgres -c "psql -At -c '$db_list_sql'"); do
su - postgres -c "vacuumdb --analyze --quiet $db"
su - postgres -c "PGOPTIONS='--client-min-messages=warning' reindexdb --quiet $db"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment