# pg_cron — Postgres extension

> Run SQL on a schedule written in cron syntax.

Source: https://scribase.com/extensions/pg_cron

Category: Jobs, queues and partitions. Version: 1.6. Optional, enabled per project.

## About

pg_cron runs SQL statements on a cron schedule from inside the database: nightly cleanups, rollups, refreshing materialized views, or calling pg_net to hit an endpoint. Every run is recorded in cron.job_run_details.

## Enable it

```sql
create extension if not exists pg_cron;
```

## Example

```sql
select cron.schedule(
  'purge-expired-sessions',
  '0 3 * * *',              -- every day at 03:00 UTC
  $$delete from sessions where expires_at < now()$$
);

select jobid, status, start_time from cron.job_run_details
order by start_time desc limit 10;

select cron.unschedule('purge-expired-sessions');
```

Upstream: https://github.com/citusdata/pg_cron