Skip to content

Instantly share code, notes, and snippets.

@dilmurodov
Forked from jgould22/Dockerfile
Created March 18, 2024 16:06
Show Gist options
  • Save dilmurodov/52cb866410d730ec0b0121448a1bed7d to your computer and use it in GitHub Desktop.
Save dilmurodov/52cb866410d730ec0b0121448a1bed7d to your computer and use it in GitHub Desktop.
Postgres 15 - Alpine - pg_partman with pg_jobmon
FROM postgres:15-alpine
LABEL maintainer="Jordan Gould <[email protected]>"
# Based on https://github.com/andreaswachowski/docker-postgres/blob/master/initdb.sh
ENV PG_JOBMON_VERSION v1.4.1
ENV PG_PARTMAN_VERSION v4.7.1
# Install pg_jobmon
RUN set -ex \
\
# Get some basic deps required to download the extensions and name them fetch-deps so we can delete them later
&& apk add --no-cache --virtual .fetch-deps \
ca-certificates \
openssl \
tar \
\
# Download pg_jobmon
&& wget -O pg_jobmon.tar.gz "https://github.com/omniti-labs/pg_jobmon/archive/$PG_JOBMON_VERSION.tar.gz" \
# Make a dir to store the src files
&& mkdir -p /usr/src/pg_jobmon \
# Extract the src files
&& tar \
--extract \
--file pg_jobmon.tar.gz \
--directory /usr/src/pg_jobmon \
--strip-components 1 \
# Delete the src tar
&& rm pg_jobmon.tar.gz \
\
# Get the depends required to build pg_jobmon and name this set of depends build-deps so we can delete them later
&& apk add --no-cache --virtual .build-deps \
autoconf \
automake \
g++ \
clang15 \
llvm15 \
libtool \
libxml2-dev \
make \
perl \
# Change to the src
&& cd /usr/src/pg_jobmon \
# Build the extenison
&& make \
# Install the extension
&& make install \
# Return to home so we are ready for the next step
&& cd / \
# Delete the src files from this step
&& rm -rf /usr/src/pg_jobmon
# Install pg_partman
RUN set -ex \
# Download pg_partman
&& wget -O pg_partman.tar.gz "https://github.com/pgpartman/pg_partman/archive/$PG_PARTMAN_VERSION.tar.gz" \
# Create a folder to put the src files in
&& mkdir -p /usr/src/pg_partman \
# Extract the src files
&& tar \
--extract \
--file pg_partman.tar.gz \
--directory /usr/src/pg_partman \
--strip-components 1 \
# Delete src file tar
&& rm pg_partman.tar.gz \
# Move to src file folder
&& cd /usr/src/pg_partman \
# Build the extension
&& make \
# Install the extension
&& make install \
# Delete the src files for pg_partman
&& rm -rf /usr/src/pg_partman \
# Delete the dependancies for downloading and building the extensions, we no longer need them
&& apk del .fetch-deps .build-deps
# Copy the init script
# The Docker Postgres initd script will run anything
# in the directory /docker-entrypoint-initdb.d
COPY initdb.sh /docker-entrypoint-initdb.d/initdb.sh
#!/bin/bash -e
echo "Creating dblink extension"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE SCHEMA dblink;
CREATE EXTENSION dblink SCHEMA dblink;
EOSQL
echo "Creating jobmon extension"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE SCHEMA jobmon;
CREATE EXTENSION pg_jobmon SCHEMA jobmon;
INSERT INTO jobmon.dblink_mapping_jobmon (username, pwd) VALUES ('$POSTGRES_USER', '$POSTGRES_PASSWORD');
EOSQL
echo "Creating partman extension"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE SCHEMA partman;
CREATE EXTENSION pg_partman SCHEMA partman;
EOSQL
echo "Adding jobmon permissions"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
GRANT USAGE ON SCHEMA jobmon TO $POSTGRES_USER;
GRANT USAGE ON SCHEMA dblink TO $POSTGRES_USER;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA jobmon TO $POSTGRES_USER;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA jobmon TO $POSTGRES_USER;
GRANT ALL ON ALL SEQUENCES IN SCHEMA jobmon TO $POSTGRES_USER;
EOSQL
echo "ADDING pg_partman_bgw TO postgresql.conf"
echo "shared_preload_libraries = 'pg_partman_bgw'" >> $PGDATA/postgresql.conf
echo "pg_partman_bgw.interval = 3600" >> $PGDATA/postgresql.conf
echo "pg_partman_bgw.role = '$POSTGRES_USER'" >> $PGDATA/postgresql.conf
echo "pg_partman_bgw.dbname = '$POSTGRES_DB'" >> $PGDATA/postgresql.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment