citusdata/pg_cron logo

citusdata/pg_cron

Free

Run periodic jobs in PostgreSQL

FreeFree tier
Type
Open Source
Company
Citus Data (part of Microsoft)

About citusdata/pg_cron

pg_cron is a simple cron-based job scheduler for PostgreSQL (version 10 or higher) that runs inside the database as a PostgreSQL extension. It uses a background worker to track jobs in the cron.job table, supporting standard cron syntax with additional capabilities such as specifying intervals in seconds (e.g., '10 seconds') and using '$' to indicate the last day of the month. pg_cron can run multiple jobs in parallel, but only one instance of each job at a time; subsequent triggers are queued until the current instance completes. Jobs are managed via built-in functions: cron.schedule, cron.schedule_in_database, cron.unschedule, and cron.alter_job. Row-level security (RLS) ensures that users can only see and modify their own jobs, unless they are a superuser or have bypass RLS privileges. This extension is ideal for automating periodic database maintenance tasks directly from within PostgreSQL without requiring external cron daemons.

Key Features

Runs inside PostgreSQL as an extension with a background worker
Supports standard cron syntax (minute, hour, day, month, weekday)
Allows intervals in seconds (e.g., '10 seconds')
Supports '$' for last day of the month
Parallel execution of multiple jobs, with queuing for overlapping instances
Job management via SQL functions: schedule, unschedule, alter_job
Job metadata stored in cron.job table (schedule, command, database, username, etc.)
Row-level security (RLS) for job visibility and isolation

Pros & Cons

Pros
  • Fully integrated into PostgreSQL – no external cron daemon needed
  • Uses familiar and widely supported cron syntax
  • Lightweight and simple to set up as a standard extension
  • Supports fine-grained scheduling down to seconds
  • Open source (licensed under PostgreSQL license)
  • Active community and maintained by Citus Data
Cons
  • Only one instance of a specific job runs at a time; overlapping executions are queued
  • Requires PostgreSQL 10 or higher
  • Schedule is limited to database-level jobs; cannot directly schedule OS commands
  • Second-based intervals cannot be combined with standard cron time fields
  • No built-in web UI – management done via SQL

Best For

Schedule periodic data cleanup or archiving tasksAutomate VACUUM, ANALYZE, or other maintenance operationsRefresh materialized views on a regular basisRun periodic data exports or importsExecute custom administrative scripts on a cron scheduleTrigger batch processing jobs within PostgreSQL

FAQ

How do I create a new cron job?
Use the cron.schedule() function. For example: SELECT cron.schedule('job-name', '* * * * *', 'VACUUM');
Can I schedule a job in a different database?
Yes, use cron.schedule_in_database() to specify the database name where the job should execute.
How do I remove a cron job?
Use the cron.unschedule() function with the job ID or job name.
Is pg_cron free to use?
Yes, pg_cron is open source and free to use under the PostgreSQL license.
Does pg_cron support intervals shorter than one minute?
Yes, you can use a seconds-based interval like '10 seconds' to run a job every 10 seconds, but you cannot combine seconds with other time fields.