Skip to content

Commit

Permalink
Add support for extensions loading and creation.
Browse files Browse the repository at this point in the history
Using POSTGRESQL_LIBRARIES and POSTGRESQL_EXTENSIONS allow
users to enable extensions (some bundled in the container) without
having to do anything but adding 2 variables.
  • Loading branch information
mscherer committed Feb 17, 2021
1 parent 204f828 commit 1382709
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 0 additions & 1 deletion examples/pgaudit/postgresql-cfg/10_pgaudit.conf

This file was deleted.

1 change: 1 addition & 0 deletions src/root/usr/bin/run-postgresql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ if $PG_INITIALIZED ; then
create_users
fi

create_extensions
process_extending_files \
"${APP_DATA}/src/postgresql-start" \
"${CONTAINER_SCRIPTS_PATH}/start"
Expand Down
9 changes: 9 additions & 0 deletions src/root/usr/share/container-scripts/postgresql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ The following environment variables influence the PostgreSQL configuration file.
Set to an estimate of how much memory is available for disk caching by the operating system and within the database itself


The following environment variables deal with extensions. They are all optional, and if not set, no extensions will be enabled.

**`POSTGRESQL_LIBRARIES`**
A comma-separated list of libraries that Postgres will preload using shared_preload_libraries.

**`POSTGRESQL_EXTENSIONS`**
A space-separated list of extensions to create when the server start. Once created, the extensions will stay even if the variable is cleared.


You can also set the following mount points by passing the `-v /host/dir:/container/dir:Z` flag to Docker.

**`/var/lib/pgsql/data`**
Expand Down
17 changes: 17 additions & 0 deletions src/root/usr/share/container-scripts/postgresql/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ function should_hack_data_sync_retry() {
return 1
}

function generate_postgresql_libraries_config() {
if [ -v POSTGRESQL_LIBRARIES ]; then
echo "shared_preload_libraries='${POSTGRESQL_LIBRARIES}'" >> "${POSTGRESQL_CONFIG_FILE}"
fi
}


# New config is generated every time a container is created. It only contains
# additional custom settings and is included from $PGDATA/postgresql.conf.
function generate_postgresql_config() {
Expand All @@ -160,6 +167,7 @@ function generate_postgresql_config() {
echo "data_sync_retry = on" >>"${POSTGRESQL_CONFIG_FILE}"
fi

generate_postgresql_libraries_config
(
shopt -s nullglob
for conf in "${APP_DATA}"/src/postgresql-cfg/*.conf; do
Expand Down Expand Up @@ -478,3 +486,12 @@ process_extending_files()
done
done <<<"$(get_matched_files '*.sh' "$@" | sort -u)"
}

create_extensions()
{
if [ -v POSTGRESQL_EXTENSIONS ]; then
for EXT in $POSTGRESQL_EXTENSIONS; do
psql -c "CREATE EXTENSION IF NOT EXISTS ${EXT};"
done;
fi
}
3 changes: 2 additions & 1 deletion test/run_test
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,8 @@ run_pgaudit_test()

DOCKER_ARGS="
-e POSTGRESQL_ADMIN_PASSWORD=password
-e POSTGRESQL_EXTENSIONS=pgaudit
-e POSTGRESQL_LIBRARIES=pgaudit
-v ${config_dir}:/opt/app-root/src:Z
-v ${data_dir}:/var/lib/pgsql/data:Z
" create_container "$name"
Expand All @@ -878,7 +880,6 @@ run_pgaudit_test()
# Deliberately moving heredoc into the container, otherwise it does not work
# in podman 1.6.x due to https://bugzilla.redhat.com/show_bug.cgi?id=1827324
docker exec -i $(get_cid "$name") bash -c "psql <<EOSQL
CREATE EXTENSION pgaudit;
SET pgaudit.log = 'read, ddl';
CREATE DATABASE pgaudittest;
EOSQL"
Expand Down

0 comments on commit 1382709

Please sign in to comment.