Search code examples
postgresqldockerpg-cron

I can't get pg_cron extension to work in latest postgres 16 image


The error I get during startup is:

waiting for server to start....2024-03-13 03:20:52.400 UTC [57] FATAL:  could not access file "cron": No such file or directory

The cron executable is in the path:

# which cron
/usr/sbin/cron

I did the following steps to setup pg_cron:

Dockerfile has these lines:

FROM docker.io/library/postgres
RUN apt-get update && \
    apt-get install -y \
    cron \
    postgresql-`postgres -V | awk '{print $3}' | awk -F. '{print $1}'`-cron

Where that last line becomes postgresql-16-cron

A startup script /docker-entrypoint-initdb.d/0001-pg_cron.sh contains these lines:

#!/bin/sh

# We need to append a line to the end of postgresql.conf to load the cron shared library
echo -e "shared_preload_libraries='cron'\ncron.database_name = 'mydb'" >> ${PGDATA}/postgresql.conf

# Dump the postgres.conf file for debugging
cat ${PGDATA}/postgresql.conf

# Execute pg_ctl restart so that postgres reloads the updated config file
pg_ctl restart

The dump of postgresql.conf shows that it does have the above lines at the end:

#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------

# Add settings for extensions here
shared_preload_libraries='cron'
cron.database_name = 'mydb'

Anyone have any success with latest postgres 16 with this extension?


Solution

  • 🗎 Dockerfile

    FROM docker.io/library/postgres
    
    RUN apt-get update && \
        apt-get install -y \
        cron \
        postgresql-`postgres -V | awk '{print $3}' | awk -F. '{print $1}'`-cron
    
    COPY setup-pg-cron.sh /docker-entrypoint-initdb.d/
    
    ENV POSTGRES_PASSWORD "password"
    

    🗎 setup-pg-cron.sh

    #!/bin/bash
    
    cat <<EOT >> /var/lib/postgresql/data/postgresql.conf
    shared_preload_libraries='pg_cron'
    cron.database_name='postgres'
    EOT
    

    Build and run.

    docker build -t postgres-cron . && docker run -it postgres-cron
    

    You should see at the end of the startup logs:

    pg_cron scheduler started