From what I can see you are not installing pg_cron anywhere. Since it is not packaged with the default Postgres Docker image you will have to care of that.
For example by extending the Image and using a build
entry in your docker-compose.yml
.
# Dockerfile relative to docker-compose.yml
FROM postgres:11.5
RUN apt-get update && apt-get -y install git build-essential postgresql-server-dev-11
RUN git clone https://github.com/citusdata/pg_cron.git
RUN cd pg_cron && make && make install
version: '3.7'
services:
pg:
container_name: pg-container
build: .
environment:
POSTGRES_DB: "pgdb"
POSTGRES_USER: "pguser"
POSTGRES_PASSWORD: "pgpass"
volumes:
- ./:/docker-entrypoint-initdb.d
ports:
- "5432:5432"
In newer versions of Postgres - you can install the pg_cron
extension with apt-get
.
FROM postgres:16-bookworm
RUN apt-get update && \
apt-get -y install postgresql-16-cron && \
apt-get clean \
&& rm -rf /var/lib/apt/lists/*
This will take care of the "installation" part. It is also necessary to configure pg_cron
and finally create the extension.
# add to postgresql.conf
# required to load pg_cron background worker on start-up
shared_preload_libraries = 'pg_cron'
# optionally, specify the database in which the pg_cron background worker should run (defaults to postgres)
cron.database_name = 'postgres'
Alternatively, with Docker Compose these configuration values can also be passed as a command
entry:
services:
db:
build: .
command:
[
"postgres",
"-c",
"log_statement=all",
"-c",
"log_destination=stderr",
"-c",
"shared_preload_libraries=pg_cron",
"-c",
"cron.database_name=${POSTGRES_DB-postgres}",
]
Finally, you will have to create the extension (in a migration script, init script or else).
-- run as superuser:
CREATE EXTENSION pg_cron;
-- optionally, grant usage to regular users:
GRANT USAGE ON SCHEMA cron TO marco;
Loading the extension and activating it (create extension...) can be done in multiple ways in Docker: https://hub.docker.com/_/postgres/
This worked for me - it probably needs some more optimization.