Skip to content

Instantly share code, notes, and snippets.

@jredrejo
Forked from mmrwoods/postgres
Last active October 9, 2017 01:56
Show Gist options
  • Save jredrejo/e577cd3c82c26dab7bc3 to your computer and use it in GitHub Desktop.
Save jredrejo/e577cd3c82c26dab7bc3 to your computer and use it in GitHub Desktop.
# dump all databases once every 24 hours
45 4 * * * root nice -n 19 su - postgres -c "pg_dumpall --clean" | gzip -9 > /var/local/backup/postgres/postgres_all.sql.gz
# or dump only ONE database every 24 hours
45 4 * * * root nice -n 19 su - postgres -c "pg_dump searchprice --clean" | gzip -9 > /var/local/backup/postgres/searchprice_db.sql.gz
# dump only ONE table every 24 hours
45 4 * * * root nice -n 19 su - postgres -c "pg_dump -t track_item_click searchprice --clean" | gzip -9 > /var/local/backup/postgres/track_item_click.sql.gz
# vacuum all databases every night (full vacuum on Sunday night, lazy vacuum every other night)
45 3 * * 1-6 root nice -n 19 su - postgres -c "vacuumdb --all --analyze --quiet"
45 3 * * 0 root nice -n 19 su - postgres -c "vacuumdb --all --full --analyze"
# or vacuum only ONE database every night (full vacuum on Sunday night, lazy vacuum every other night)
45 3 * * 1-6 root nice -n 19 su - postgres -c "vacuumdb searchprice --analyze --quiet"
45 3 * * 0 root nice -n 19 su - postgres -c "vacuumdb searchprice --full --analyze"
# re-index all databases once a week
0 3 * * 0 root nice -n 19 su - postgres -c 'psql -t -c "select datname from pg_database order by datname;" | xargs -n 1 -I"{}" -- psql -U postgres {} -c "reindex database {};"'
# or re-index one databases once a week
0 3 * * 0 root nice -n 19 su - postgres -c 'psql -U postgres -d searchprice -c "reindex database searchprice;"'
#or re-index some tables once a week
0 3 * * 0 root nice -n 19 su - postgres -c 'psql -U postgres -d searchprice -c "reindex table property;reindex table item;"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment