On this page

Cron jobs

Run SQL on a schedule with pg_cron. Jobs run in your database as your project role.

Cron is ready in every project: enable pg_cron under Database → Extensions (or run create extension pg_cron, which turns it on for you and then reports that no CREATE EXTENSION is needed). Manage jobs in Studio under Integrations → Cron jobs or with SQL.

#Schedule a job

SQL
-- Every minute
select cron.schedule('tick', '* * * * *', $$insert into public.ticks default values$$);

-- Every day at 03:00 UTC
select cron.schedule('nightly-cleanup', '0 3 * * *', $$
  delete from public.events where created_at < now() - interval '30 days'
$$);

-- Every 30 seconds
select cron.schedule('poll', '30 seconds', 'select public.process_outbox()');

Schedules use cron syntax (minute, hour, day of month, month, day of week) in UTC, or 'N seconds' for 1 to 59 seconds. Scheduling a job with an existing name replaces it.

#List, change and remove jobs

SQL
select jobid, jobname, schedule, active from cron.job;

select cron.alter_job((select jobid from cron.job where jobname = 'tick'), schedule => '*/5 * * * *');
select cron.alter_job((select jobid from cron.job where jobname = 'tick'), active => false);

select cron.unschedule('tick');

select jobid, status, return_message, start_time, end_time
from cron.job_run_details order by start_time desc limit 20;

#In Studio

Integrations → Cron jobs creates, edits, pauses and deletes jobs and shows the most recent 200 runs with their status and output. To get an alert when a job does not run on time or fails, add a monitor for it in Job monitoring.

#How it works

  • Jobs run on the platform scheduler, always in your project's database and as your project's role. cron.schedule_in_database only accepts your own database.
  • Up to 20 jobs per project. A run that takes longer than 30 minutes is cancelled.
  • Run history (cron.job_run_details) is kept for 7 days.
  • Jobs move with your project when it changes server and are removed when the project is deleted. Disabling pg_cron removes all jobs.