| +-------------- 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)
| | | | |
| | | | |
* * * * *
An easy way to create a cron schedule is: crontab.guru.
The code in pg_cron that handles parsing and scheduling comes directly from
the cron source code by Paul Vixie, hence the same options are supported. Be
aware that pg_cron always uses GMT!
Installing pg_cron
Install on Red Hat, CentOS, Fedora, Amazon Linux with PostgreSQL 12 using
PGDG:
# Install the pg_cron extension
sudo yum install -y pg_cron_12
Install on Debian, Ubuntu with PostgreSQL 12 using apt.postgresql.org:
# Install the pg_cron extension
sudo apt-get -y install postgresql-12-cron
You can also install pg_cron by building it from source:
git clone https://github.com/citusdata/pg_cron.git
cd pg_cron
# Ensure pg_config is in your path, e.g.
export PATH=/usr/pgsql-12/bin:$PATH
make && sudo PATH=$PATH make install
Setting up pg_cron
To start the pg_cron background worker when PostgreSQL starts, you need
to add pg_cron to
shared_preload_libraries
in postgresql.conf. Note that
pg_cron does not run any jobs as a long a server is in hot standby mode, but it
automatically starts when the server is promoted.
By default, the pg_cron background worker expects its metadata tables to be
created in the “postgres” database. However, you can configure this by setting
the cron.database_name configuration parameter in postgresql.conf.
2
评论