Supabase Cron Job Setup Guide¶
A guide for setting up and configuring scheduled cron jobs in Supabase using the Dashboard.
Prerequisites¶
- A Supabase project with the
pg_cronextension enabled - Access to the Supabase Dashboard
Navigate to Cron Jobs¶
- Open your Supabase project dashboard
- Go to Integrations → Cron → Jobs
- Click Create job or navigate directly to the new job form
Configure Your Cron Job¶
Job Name¶
Enter a descriptive name for your job (e.g., upload_try_week_clean_job).
Important: Job names are case-sensitive and cannot be renamed after creation. Creating a second job with the same name will overwrite the first.
Schedule¶
Enter a cron expression or use the "Use natural language" toggle for presets.
Cron Syntax Reference¶
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sunday=0)
│ │ │ │ │
* * * * *
Common Examples¶
| Expression | Description |
|---|---|
0 0 * * 0 |
Every Sunday at midnight (GMT) |
0 0 * * 1 |
Every Monday at midnight (GMT) |
0 2 * * 1 |
Every Monday at 2:00 AM (GMT) |
*/5 * * * * |
Every 5 minutes |
0 0 1 * * |
First day of each month at midnight |
0 3 * * 6 |
Every Saturday at 3:00 AM (GMT) |
30 seconds |
Every 30 seconds (requires Postgres 15.1.1.61+) |
Job Type¶
Choose one of the following:
| Type | Description | Requirements |
|---|---|---|
| SQL Snippet | Execute raw SQL statements | None |
| Database function | Call an existing Postgres function | Function must exist |
| HTTP Request | Send HTTP requests to external URLs | pg_net extension |
| Supabase Edge Function | Trigger a Supabase Edge Function | pg_net extension |
SQL Snippet Example¶
For a weekly reset of upload attempt counters:
UPDATE users
SET avatar_upload_tries = 0;
Save the Job¶
Click Save cron job to activate your scheduled job.
Managing Existing Jobs¶
Edit a Job¶
- Find the job in the Jobs list
- Click the three-dot menu (⋮) on the right
- Select Edit cron job
- Make changes and click Save cron job
Activate/Deactivate a Job¶
Toggle the Active/Inactive switch next to the job name.
Delete a Job¶
- Click the three-dot menu (⋮) on the right
- Select Delete cron job
- Confirm by typing the job name
View Job History¶
Click the History button next to the job name to inspect past runs.
Installing pg_net Extension¶
Required for HTTP requests and Edge Function triggers:
- In the cron job form, click Install pg_net extension
- Or run via SQL:
CREATE EXTENSION IF NOT EXISTS pg_net WITH SCHEMA extensions;
Advanced Examples¶
Delete Old Data Weekly¶
SELECT cron.schedule(
'saturday-cleanup',
'30 3 * * 6', -- Saturday at 3:30 AM (GMT)
$$ DELETE FROM events WHERE event_time < now() - interval '1 week' $$
);
Run Vacuum Daily¶
SELECT cron.schedule('nightly-vacuum', '0 3 * * *', 'VACUUM');
Call a Database Function¶
SELECT cron.schedule('call-db-function', '*/5 * * * *', 'SELECT hello_world()');
Invoke Edge Function¶
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',
'Authorization', 'Bearer ' || 'YOUR_ANON_KEY'
),
body := jsonb_build_object('time', now()),
timeout_milliseconds := 5000
) AS request_id;
$$
);
Caution¶
Be careful when scheduling system maintenance tasks. For example, terminating idle connections with pg_terminate_backend(pid) can disrupt background processes like nightly backups.
Consider using existing Postgres settings (like idle_session_timeout) instead of custom cron jobs for common maintenance tasks.