Skip to content

Commit

Permalink
feat: add pg_cron extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazmi35 committed Jan 6, 2025
1 parent 8b3a3f0 commit 5653f8c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
25 changes: 24 additions & 1 deletion 16/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,40 @@ RUN mkdir -p /tmp/lib /tmp/share /tmp/bin \
&& cp -r $(ls . | grep timescaledb-) /tmp/bin

RUN echo -e "[INFO] TimescaleDB lib files to be installed: \n$(ls /tmp/lib)"
RUN echo -e "[INFO] TimescaleDB share files to be installed: \n$(ls /tmp/share)"

# PostgreSQL Server
# PostgreSQL Server (base image)
FROM docker.io/bitnami/postgresql:16.6.0 AS postgresql

# pg_cron
FROM postgresql AS pg_cron

# Switch to root
USER root

# Select and copy the pg_cron extension files
RUN install_packages --no-install-recommends git gcc make llvm clang \
&& git clone https://github.com/citusdata/pg_cron.git \
&& cd pg_cron \
&& make install

RUN echo -e "[INFO] pg_cron lib files to be installed: \n$(ls /opt/bitnami/postgresql/lib | grep pg_cron)"
RUN echo -e "[INFO] pg_cron share files to be installed: \n$(ls /opt/bitnami/postgresql/share/extension | grep pg_cron)"

# PostgreSQL Server
FROM postgresql AS final

# TimescaleDB tools
COPY --from=timescaledb /tmp/bin/* /usr/local/bin/

# TimescaleDB extension (We will install last 3 versions)
COPY --from=timescaledb /tmp/lib/* /opt/bitnami/postgresql/lib/
COPY --from=timescaledb /tmp/share/* /opt/bitnami/postgresql/share/extension/

# pg_cron extension
COPY --from=pg_cron /opt/bitnami/postgresql/lib/pg_cron* /opt/bitnami/postgresql/lib/
COPY --from=pg_cron /opt/bitnami/postgresql/share/extension/pg_cron* /opt/bitnami/postgresql/share/extension/

# TimescaleDB Docker initialization scripts
COPY --from=timescaledb /docker-entrypoint-initdb.d/*.sh /docker-entrypoint-initdb.d/
COPY ./initdb/* /docker-entrypoint-initdb.d/
Expand Down
24 changes: 23 additions & 1 deletion 17/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,26 @@ RUN mkdir -p /tmp/lib /tmp/share /tmp/bin \

RUN echo -e "[INFO] TimescaleDB lib files to be installed: \n$(ls /tmp/lib)"

# PostgreSQL Server (base image)
FROM bitnami/postgresql:17.2.0 AS postgresql

# pg_cron
FROM postgresql AS pg_cron

# Switch to root
USER root

# Select and copy the pg_cron extension files
RUN install_packages --no-install-recommends git gcc make llvm clang \
&& git clone https://github.com/citusdata/pg_cron.git \
&& cd pg_cron \
&& make install

RUN echo -e "[INFO] pg_cron lib files to be installed: \n$(ls /opt/bitnami/postgresql/lib | grep pg_cron)"
RUN echo -e "[INFO] pg_cron share files to be installed: \n$(ls /opt/bitnami/postgresql/share/extension | grep pg_cron)"

# PostgreSQL Server
FROM docker.io/bitnami/postgresql:17.2.0 AS postgresql
FROM postgresql AS final

# TimescaleDB tools
COPY --from=timescaledb /tmp/bin/* /usr/local/bin/
Expand All @@ -25,6 +43,10 @@ COPY --from=timescaledb /tmp/bin/* /usr/local/bin/
COPY --from=timescaledb /tmp/lib/* /opt/bitnami/postgresql/lib/
COPY --from=timescaledb /tmp/share/* /opt/bitnami/postgresql/share/extension/

# pg_cron extension
COPY --from=pg_cron /opt/bitnami/postgresql/lib/pg_cron* /opt/bitnami/postgresql/lib/
COPY --from=pg_cron /opt/bitnami/postgresql/share/extension/pg_cron* /opt/bitnami/postgresql/share/extension/

# TimescaleDB Docker initialization scripts
COPY --from=timescaledb /docker-entrypoint-initdb.d/*.sh /docker-entrypoint-initdb.d/
COPY ./initdb/* /docker-entrypoint-initdb.d/
Expand Down
11 changes: 8 additions & 3 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
# shared preload list, or else it gets overwritten.
if [ -z "$POSTGRESQL_SHARED_PRELOAD_LIBRARIES" ]
then
POSTGRESQL_SHARED_PRELOAD_LIBRARIES=timescaledb
POSTGRESQL_SHARED_PRELOAD_LIBRARIES=timescaledb,pg_cron
else
POSTGRESQL_SHARED_PRELOAD_LIBRARIES="$POSTGRESQL_SHARED_PRELOAD_LIBRARIES,timescaledb"
POSTGRESQL_SHARED_PRELOAD_LIBRARIES="$POSTGRESQL_SHARED_PRELOAD_LIBRARIES,timescaledb,pg_cron"
fi
export POSTGRESQL_SHARED_PRELOAD_LIBRARIES

# If it's already init, run timescaledb-tune again (to update the config if needed)
# If it's already init, run timescaledb-tune and update pg_cron db (to update the config if needed)
if [ -f "$POSTGRESQL_VOLUME_DIR/.user_scripts_initialized" ]; then
# Run timescaledb-tune script
info "Running timescaledb-tune to update the configuration..."
/docker-entrypoint-initdb.d/001_timescaledb_tune.sh
info "timescaledb-tune finished"
fi

# If pg_cron config file does not exist, create it
if [ ! -f "$POSTGRESQL_CONF_DIR/conf.d/pg_cron.conf" ]; then
echo "cron.database_name = '$POSTGRESQL_DATABASE'" > "$POSTGRESQL_CONF_DIR/conf.d/pg_cron.conf"
fi

# Fall through to the original entrypoint. Note that we use exec here because
# this wrapper script shouldn't change PID 1 of the container.
exec "/opt/bitnami/scripts/postgresql/entrypoint.sh" "$@"
16 changes: 16 additions & 0 deletions initdb/001_pg_cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

. /opt/bitnami/scripts/liblog.sh
. /opt/bitnami/scripts/postgresql-env.sh

export PGPASSWORD="$POSTGRESQL_PASSWORD"

if [ "${POSTGRESQL_POSTGRES_PASSWORD}" != "" ]; then
export PGPASSWORD=${POSTGRESQL_POSTGRES_PASSWORD}
fi

# Install pg_cron extension
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS pg_cron"

# Configure pg_cron to use POSTGRESQL_DATABASE
echo "cron.database_name = '$POSTGRESQL_DATABASE'" > $POSTGRESQL_CONF_DIR/conf.d/pg_cron.conf

0 comments on commit 5653f8c

Please sign in to comment.