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 authored and phracek committed Oct 3, 2023
1 parent 481efd6 commit d7fc4cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
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 @@ -74,6 +74,15 @@ Set to an estimate of how much memory is available for disk caching by the opera
**`POSTGRESQL_LOG_DESTINATION (default: /var/lib/pgsql/data/userdata/log/postgresql-*.log)`**
Where to log errors, the default is `/var/lib/pgsql/data/userdata/log/postgresql-*.log` and this file is rotated; it can be changed to `/dev/stderr` to make debugging easier

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 @@ -145,6 +145,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 Down Expand Up @@ -172,6 +179,7 @@ function generate_postgresql_config() {
echo "log_filename = '$(basename "${POSTGRESQL_LOG_DESTINATION}")'" >>"${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 @@ -503,3 +511,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
}
18 changes: 16 additions & 2 deletions test/run_test
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ run_s2i_enable_ssl_test
run_upgrade_test
run_migration_test
run_pgaudit_test
<<<<<<< HEAD
run_logging_test
||||||| parent of bcc9062 (Add support for extensions loading and creation.)
=======
run_new_pgaudit_test
>>>>>>> bcc9062 (Add support for extensions loading and creation.)
"

test $# -eq 1 -a "${1-}" == --list && echo "$TEST_LIST" && exit 0
Expand All @@ -37,6 +42,8 @@ test -n "${VERSION-}" || false 'make sure $VERSION is defined'
test -n "${OS-}" || false 'make sure $OS is defined'


DOCKER_EXTRA_ARGS=

volumes_to_clean=
images_to_clean=()
files_to_clean=
Expand Down Expand Up @@ -897,7 +904,7 @@ run_pgaudit_test()
# create a dir for config
config_dir=$(mktemp -d --tmpdir pg-hook-volume.XXXXX)
add_cleanup_command /bin/rm -rf "$config_dir"
cp -r "$test_dir"/examples/pgaudit/* "$config_dir"
$DOCKER_EXTRA_ARGS || cp -r "$test_dir"/examples/pgaudit/* "$config_dir"
setfacl -R -m u:26:rwx "$config_dir"

# create a dir for data
Expand All @@ -906,6 +913,7 @@ run_pgaudit_test()

DOCKER_ARGS="
-e POSTGRESQL_ADMIN_PASSWORD=password
$DOCKER_EXTRA_ARGS
-v ${config_dir}:/opt/app-root/src:Z
-v ${data_dir}:/var/lib/pgsql/data:Z
" create_container "$name"
Expand All @@ -917,7 +925,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" || ret=2
Expand Down Expand Up @@ -983,6 +990,13 @@ run_logging_test()
echo " Success!"
}

run_new_pgaudit_test() {
DOCKER_EXTRA_ARGS="
-e POSTGRESQL_EXTENSIONS=pgaudit
-e POSTGRESQL_LIBRARIES=pgaudit"
run_pgaudit_test
}

# configuration defaults
POSTGRESQL_MAX_CONNECTIONS=100
POSTGRESQL_MAX_PREPARED_TRANSACTIONS=0
Expand Down

0 comments on commit d7fc4cf

Please sign in to comment.