Files
supabase/apps/reference/docs/guides/database/extensions/pgcron.mdx
Copple ca63127db1 Docs: Adds pgnet and database webhooks (#9803)
* Adds pgnet and database webhooks

* fixes name of pg_net

* Adds the cron extension

* Update apps/reference/docs/guides/database/database-webhooks.mdx

Co-authored-by: Steve Chavez <stevechavezast@gmail.com>

* Update apps/reference/docs/guides/database/extensions/pgnet.mdx

Co-authored-by: Steve Chavez <stevechavezast@gmail.com>

* Update apps/reference/docs/guides/database/database-webhooks.mdx

Co-authored-by: dng <danny@supabase.io>

* Update apps/reference/docs/guides/database/database-webhooks.mdx

Co-authored-by: dng <danny@supabase.io>

* Update apps/reference/docs/guides/database/database-webhooks.mdx

Co-authored-by: dng <danny@supabase.io>

* Update apps/reference/nav/_referenceSidebars.js

Co-authored-by: dng <danny@supabase.io>

* Update apps/reference/nav/_referenceSidebars.js

Co-authored-by: dng <danny@supabase.io>

* Update apps/reference/docs/guides/database/extensions/pgnet.mdx

* Update apps/reference/docs/guides/database/extensions/pgnet.mdx

* Update apps/reference/docs/guides/database/database-webhooks.mdx

* Update apps/reference/docs/guides/database/database-webhooks.mdx

* Update apps/reference/docs/guides/database/extensions/pgnet.mdx

Co-authored-by: Steve Chavez <stevechavezast@gmail.com>
Co-authored-by: dng <danny@supabase.io>
2022-10-26 12:39:53 +00:00

100 lines
2.6 KiB
Plaintext

---
id: pgcron
title: 'pg_cron: Job Scheduling'
description: 'pgnet: a simple cron-based job scheduler for PostgreSQL that runs inside the database.'
---
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
The `pg_cron` extension is a simple cron-based job scheduler for PostgreSQL that runs inside the database.
## Usage
### Enable the extension
<Tabs
defaultValue="dashboard"
values={[
{label: 'Dashboard', value: 'dashboard'},
{label: 'SQL', value: 'sql'},
]}>
<TabItem value="dashboard">
1. Go to the [Database](https://app.supabase.com/project/_/database/tables) page in the Dashboard.
2. Click on **Extensions** in the sidebar.
3. Search for "pg_cron" and enable the extension.
</TabItem>
<TabItem value="sql">
```sql
-- Example: enable the "pg_cron" extension
create extension pg_cron with schema extensions;
-- If you're planning to use a non-superuser role to schedule jobs,
-- ensure that they are granted access to the cron schema and its underlying objects beforehand.
-- Failure to do so would result in jobs by these roles to not run at all.
grant usage on schema cron to {{DB user}};
grant all privileges on all tables in schema cron to {{DB user}};
-- Example: disable the "pg_cron" extension
drop extension if exists pg_cron;
```
</TabItem>
</Tabs>
### Syntax
The schedule uses the standard cron syntax, in which \* means "run every time period", and a specific number means "but only at this time":
```bash
┌───────────── min (0 - 59)
│ ┌────────────── hour (0 - 23)
│ │ ┌─────────────── day of month (1 - 31)
│ │ │ ┌──────────────── month (1 - 12)
│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
│ │ │ │ │ Saturday, or use names; 7 is also Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * *
```
## Examples
### Delete data every week
Delete old data on Saturday at 3:30am (GMT):
```sql
select cron.schedule (
'webhook-every-minute', -- name of the cron job
'* * * * *', -- every minute
$$ delete from events where event_time < now() - interval '1 week' $$
);
```
### Run a vacuum every day
Vacuum every day at 3:00am (GMT)
```sql
SELECT cron.schedule('nightly-vacuum', '0 3 * * *', 'VACUUM');
```
### Unschedule a job
Unschedules a job called `'nightly-vacuum'`
```sql
SELECT cron.unschedule('nightly-vacuum');
```
## Resources
- [pg_cron GitHub Repository](https://github.com/citusdata/pg_cron)