Postgresql VACUUM and ANALYZE all tables in a database

The example below uses the database named netbox so change as needed. It gets a list of the tables in the database and for each one performs a full vacuum and analyze. There is probably a more efficient way to do this from the psql prompt but this is what worked for me:

su - postgres
for TABLE in $(psql -d netbox -c "\dt" | grep table  | awk -F "|" '{print $2}' | tr -d " "); do psql -d netbox -c "VACUUM FULL ANALYZE VERBOSE $TABLE" ; done

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>