Skip to content

Instantly share code, notes, and snippets.

@mmrwoods
Created April 14, 2014 07:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mmrwoods/10623668 to your computer and use it in GitHub Desktop.
Save mmrwoods/10623668 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