|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: Setting up and using the pgcron extension |
| 4 | + description: This page explains how to set up and use the pgcron extension. |
| 5 | +content: |
| 6 | + h1: Setting up and using the pgcron extension |
| 7 | + paragraph: This page explains how to set up and use the pgcron extension. |
| 8 | +tags: managed-database postgresql pgcron pg-extensions |
| 9 | +dates: |
| 10 | + validation: 2025-02-17 |
| 11 | + posted: 2025-02-17 |
| 12 | +categories: |
| 13 | + - managed-databases |
| 14 | + - postgresql-and-mysql |
| 15 | +--- |
| 16 | + |
| 17 | +The pg_cron extension for PostgreSQL is used to execute periodic tasks. You can schedule SQL tasks, such as queries and data imports, to run at regular intervals. This allows you to automate the tasks to be run regularly, on a daily, weekly or monthly basis, for example. |
| 18 | + |
| 19 | +The `pgcron` extension is available with Scaleway Managed Databases for PostgreSQL. The extension is natively loaded in the `shared_preload_libraries` of the Database Instances by default. |
| 20 | + |
| 21 | +<Macro id="requirements" /> |
| 22 | + |
| 23 | +- A Scaleway account logged into the [console](https://console.scaleway.com) |
| 24 | +- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 25 | +- A [Database Instance](/managed-databases-for-postgresql-and-mysql/how-to/create-a-database/) running a PostgreSQL engine |
| 26 | + |
| 27 | +## Installing pgcron |
| 28 | + |
| 29 | + |
| 30 | +1. Run the following command to install the extension: |
| 31 | + |
| 32 | +```sql |
| 33 | +rdb=> CREATE EXTENSION pg_cron; |
| 34 | +CREATE EXTENSION |
| 35 | +``` |
| 36 | + |
| 37 | +## Configuring pg_cron |
| 38 | + |
| 39 | +<Message type="note"> |
| 40 | + To fully use the extension, you must grant read/write rights to the user who will be running the `pg_cron` functions to manage jobs on the database. Refer to the [How to add users to a PostgreSQL or MySQL Database Instance](/managed-databases-for-postgresql-and-mysql/how-to/add-users) documentation for more information. |
| 41 | +</Message> |
| 42 | + |
| 43 | +## Scheduling jobs |
| 44 | + |
| 45 | +To schedule jobs, you can run the following commands: |
| 46 | + |
| 47 | +```sql |
| 48 | +-- Delete old data on Saturday at 3:30am (GMT) |
| 49 | +SELECT cron.schedule('30 3 * * 6', $$DELETE FROM events WHERE event_time < now() - interval '1 week'$$); |
| 50 | +``` |
| 51 | + |
| 52 | +```sql |
| 53 | +-- Change to vacuum at 3:00am (GMT) |
| 54 | +SELECT cron.schedule('nightly-vacuum', '0 10 * * *', 'VACUUM'); |
| 55 | +``` |
| 56 | + |
| 57 | +**Listing Jobs** |
| 58 | + |
| 59 | +To list all scheduled jobs, you can run the following command: |
| 60 | + |
| 61 | +```sql |
| 62 | +SELECT * FROM cron.job; |
| 63 | +-[ RECORD 1 ]------------------------------------------------------------- |
| 64 | +jobid | 1 |
| 65 | +schedule | 30 3 * * 6 |
| 66 | +command | DELETE FROM events WHERE event_time < now() - interval '1 week' |
| 67 | +nodename | /var/run/postgresql |
| 68 | +nodeport | 5432 |
| 69 | +database | rdb |
| 70 | +username | myuser |
| 71 | +active | t |
| 72 | +jobname | |
| 73 | +-[ RECORD 2 ]------------------------------------------------------------- |
| 74 | +jobid | 2 |
| 75 | +schedule | 0 10 * * * |
| 76 | +command | VACUUM |
| 77 | +nodename | /var/run/postgresql |
| 78 | +nodeport | 5432 |
| 79 | +database | rdb |
| 80 | +username | myuser |
| 81 | +active | t |
| 82 | +jobname | nightly-vacuum |
| 83 | +``` |
| 84 | + |
| 85 | +**Unscheduling Jobs** |
| 86 | + |
| 87 | +To unschedule a job, you can run the following command: |
| 88 | + |
| 89 | +```sql |
| 90 | +SELECT cron.unschedule(1); |
| 91 | +unschedule |
| 92 | +------------ |
| 93 | +t |
| 94 | +(1 row) |
| 95 | +``` |
| 96 | + |
| 97 | +or |
| 98 | + |
| 99 | +```sql |
| 100 | +SELECT cron.unschedule('nightly-vacuum'); |
| 101 | +unschedule |
| 102 | +------------ |
| 103 | +t |
| 104 | +(1 row) |
| 105 | +``` |
| 106 | + |
| 107 | +**Scheduling Jobs in Other Databases** |
| 108 | + |
| 109 | +To schedule a job in another database, you can use the `schedule_in_database` function: |
| 110 | + |
| 111 | +```sql |
| 112 | +SELECT cron.schedule_in_database('job-in-another-db', '0 12 * * *', 'INSERT INTO public.another_db_table values (now())', 'mydb'); |
| 113 | +schedule |
| 114 | +---------- |
| 115 | +3 |
| 116 | +``` |
| 117 | + |
| 118 | +Note that cron.schedule_in_database runs jobs as the user who created them, so you'll need to connect as that user to execute the job. |
| 119 | +Additionally, this function doesn't allow specifying a different user, as it would require superuser privileges. |
| 120 | + |
| 121 | +**Editing Jobs** |
| 122 | + |
| 123 | +To edit a job, you can use the `alter_job` function: |
| 124 | + |
| 125 | +```sql |
| 126 | +SELECT cron.alter_job(job_id:=3,database:='anotherdb'); |
| 127 | +-[ RECORD 1 ] |
| 128 | +alter_job | |
| 129 | +``` |
| 130 | + |
| 131 | +**Cron Syntax** |
| 132 | + |
| 133 | +The schedule uses the standard cron syntax: |
| 134 | + |
| 135 | +``` |
| 136 | +┌───────────── min (0 - 59) |
| 137 | +│ ┌────────────── hour (0 - 23) |
| 138 | +│ │ ┌─────────────── day of month (1 - 31) or last day of the month ($) |
| 139 | +│ │ │ ┌──────────────── month (1 - 12) |
| 140 | +│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to |
| 141 | +│ │ │ │ │ Saturday, or use names; 7 is also Sunday) |
| 142 | +│ │ │ │ │ |
| 143 | +│ │ │ │ │ |
| 144 | +* * * * * |
| 145 | +``` |
| 146 | + |
| 147 | +**Time Zone** |
| 148 | + |
| 149 | +The time zone of the pg_cron extension can be changed in the advanced settings of the instance: `cron.timezone`. By default, the time zone is set to GMT. |
0 commit comments