crontool
Paste a schedule, read it in English, see the next runs.

Run VACUUM on PostgreSQL with Cron

To run PostgreSQL maintenance on a schedule, call VACUUM ANALYZE via psql from cron. Postgres has autovacuum, but a scheduled VACUUM ANALYZE can help on heavy-write tables and refresh planner statistics at a quiet hour.

The crontab line

Run crontab -e and add (4 AM every Sunday):

0 4 * * 0 /usr/bin/psql -U myuser -d mydb -c "VACUUM ANALYZE;" >> /var/log/pg-vacuum.log 2>&1

Password handling

Use a ~/.pgpass file (host:port:db:user:password, chmod 600) so psql authenticates without a prompt.

VACUUM vs VACUUM FULL

Plain VACUUM ANALYZE is online and non-blocking — safe to schedule. VACUUM FULL rewrites the table and takes an exclusive lock; only run it in a maintenance window, never as a routine cron on a live table.

Do you even need it?

Autovacuum handles most workloads. Add a scheduled VACUUM only for tables with heavy churn or if you see bloat; always keep autovacuum enabled.

FAQ

How do I schedule VACUUM in PostgreSQL with cron?

0 4 * * 0 psql -U user -d db -c "VACUUM ANALYZE;" runs weekly at 4 AM Sunday. Use ~/.pgpass for the password.

Is it safe to run VACUUM from cron on a live database?

Plain VACUUM ANALYZE is online and non-blocking, so yes. Avoid VACUUM FULL in routine cron — it locks the table exclusively.

Do I need scheduled VACUUM if autovacuum is on?

Usually not. Keep autovacuum enabled; add a scheduled VACUUM only for heavy-write tables or visible bloat.