Quickstart
Note
Job names are case sensitive and cannot be edited once created.
Attempting to create a second Job with the same name (and case) will overwrite the first Job.
Schedule a job
- Go to the Jobs section to schedule your first Job.
- Click on
Create jobbutton or navigate to the new Cron Job form here. - Name your Cron Job.
- Choose a schedule for your Job by inputting cron syntax (refer to the syntax chart in the form) or natural language.
- Input SQL snippet or select a Database function, HTTP request, or Supabase Edge Function.
-- Cron Job name cannot be edited
select cron.schedule('permanent-cron-job-name', '30 seconds', 'CALL do_something()');Note
You can input seconds for your Job schedule interval as long as you're on Postgres version 15.1.1.61 or later.
Edit a job
- Go to the Jobs section and find the Job you'd like to edit.
- Click on the three vertical dots menu on the right side of the Job and click
Edit cron job. - Make your changes and then click
Save cron job.
select cron.alter_job(
job_id := (select jobid from cron.job where jobname = 'permanent-cron-job-name'),
schedule := '*/5 * * * *'
);Full options for the cron.alter_job() function are:
cron.alter_job(
job_id bigint,
schedule text default null,
command text default null,
database text default null,
username text default null,
active boolean default null
)It is also possible to modify a job by using the cron.schedule() function by inputting the same job name. This will replace the existing job via upsert.
Activate/Deactivate a job
- Go to the Jobs section and find the Job you'd like to unschedule.
- Toggle the
Active/Inactiveswitch next to Job name.
-- Activate Job
select cron.alter_job(
job_id := (select jobid from cron.job where jobname = 'permanent-cron-job-name'),
active := true
);
-- Deactivate Job
select cron.alter_job(
job_id := (select jobid from cron.job where jobname = 'permanent-cron-job-name'),
active := false
);Unschedule a job
- Go to the Jobs section and find the Job you'd like to delete.
- Click on the three vertical dots menu on the right side of the Job and click
Delete cron job. - Confirm deletion by entering the Job name.
select cron.unschedule('permanent-cron-job-name');Caution
Unscheduling a Job will permanently delete the Job from cron.job table but its run history remain in cron.job_run_details table.
Inspecting job runs
- Go to the Jobs section and find the Job you want to see the runs of.
- Click on the
Historybutton next to the Job name.
select
*
from cron.job_run_details
where jobid = (select jobid from cron.job where jobname = 'permanent-cron-job-name')
order by start_time desc
limit 10;Caution
The records in the cron.job_run_details table are not cleaned up automatically. They are also not removed when jobs are unscheduled, which will take up disk space in your database. Schedule a cleanup Job to remove old records (see Clean up job run history below).
Examples
Delete data every week
Delete old data every Saturday at 3:30AM (GMT):
select cron.schedule (
'saturday-cleanup', -- name of the cron job
'30 3 * * 6', -- Saturday at 3:30AM (GMT)
$$ delete from events where event_time < now() - interval '1 week' $$
);Clean up job run history
cron.job_run_details grows with every Job run and is never cleaned up automatically, even after a Job is unscheduled. Schedule a Job to delete old records, keeping only the last 7 days:
select cron.schedule(
'job-run-details-cleanup', -- name of the cron job
'0 0 * * *', -- daily at midnight (GMT)
$$ delete from cron.job_run_details where end_time < now() - interval '7 days' $$
);Run a vacuum every day
Vacuum every day at 3:00AM (GMT):
select cron.schedule('nightly-vacuum', '0 3 * * *', 'VACUUM');Call a database function every 5 minutes
Create a hello_world() database function and then call it every 5 minutes:
select cron.schedule('call-db-function', '*/5 * * * *', 'SELECT hello_world()');Call a database stored procedure
To use a stored procedure, you can call it like this:
select cron.schedule('call-db-procedure', '*/5 * * * *', 'CALL my_procedure()');Invoke Supabase Edge Function every 30 seconds
Make a POST request to a Supabase Edge Function every 30 seconds:
select
cron.schedule(
'invoke-function-every-half-minute',
'30 seconds',
$$
select
net.http_post(
url:='https://project-ref.supabase.co/functions/v1/function-name',
headers:=jsonb_build_object('Content-Type','application/json', 'apikey', 'YOUR_PUBLISHABLE_KEY'),
body:=jsonb_build_object('time', now() ),
timeout_milliseconds:=5000
) as request_id;
$$
);Note
This requires the pg_net extension to be enabled.
Caution: Scheduling system maintenance
Be extremely careful when setting up Jobs for system maintenance tasks as they can have unintended consequences.
For instance, scheduling a command to terminate idle connections with pg_terminate_backend(pid) can disrupt critical background processes like nightly backups. Often, there is an existing Postgres setting, such as idle_session_timeout, that can perform these common maintenance tasks without the risk.
Reach out to Supabase Support if you're unsure if that applies to your use case.