Scheduling lets PostgreSQL run SQL statements automatically at defined times without manual execution.
Automated schedules generate reports, refresh aggregates, and purge old data without human intervention, saving time and preventing missed jobs.
pg_cron and pgAgent are the two most-used tools.pg_cron is lightweight and lives inside the database; pgAgent is part of pgAdmin and runs as an external service.
Choose pg_cron for cloud-hosted clusters (Amazon RDS, Crunchy Bridge) or when you need simple cron-style schedules and fewer dependencies.
Pick pgAgent for complex, conditional workflows, multi-step jobs, or when your organization already relies on pgAdmin.
1. CREATE EXTENSION pg_cron;
2. Set shared_preload_libraries = 'pg_cron' in postgresql.conf and restart.
3.Grant usage: GRANT USAGE ON SCHEMA cron TO reporting_role;
Use SELECT cron.schedule(schedule_name, cron_expression, sql_text[, run_as]);. The job runs under the specified role and logs to cron.job_run_details.
Update with cron.alter_job(jobid, new_schedule, new_command). Remove with cron.unschedule(jobid).
Scope each job to a low-privileged role, add explicit timeouts, and log results to a dedicated table for monitoring.
.
Jobs run in their own background worker. Keep heavy queries outside peak hours and set statement timeouts to minimize impact.
Yes. Use cron.schedule to call VACUUM on large tables during low traffic windows to reduce bloat.
pg_cron writes execution details to cron.job_run_details. Aggregate this table or ship logs externally for long-term retention.