From 4f7cb0296018f6b6aaf40fa9356c17cc8c8da247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 25 Jul 2023 11:07:31 +0200 Subject: [PATCH] Change: Create role and extensions only if they don't exist in container Avoid errors when starting an already existing and set up database. --- .docker/start-postgresql.sh | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/.docker/start-postgresql.sh b/.docker/start-postgresql.sh index 905b7d0..889975d 100644 --- a/.docker/start-postgresql.sh +++ b/.docker/start-postgresql.sh @@ -37,11 +37,26 @@ if [ -z "$DB_EXISTS" ]; then createdb --host=/tmp -O "$POSTGRES_DB" "$POSTGRES_USER" fi; -psql --host=/tmp -d $POSTGRES_DB -c "create role dba with superuser noinherit;" -psql --host=/tmp -d $POSTGRES_DB -c "grant dba to $POSTGRES_USER;" -psql --host=/tmp -d $POSTGRES_DB -c 'create extension "uuid-ossp";' -psql --host=/tmp -d $POSTGRES_DB -c 'create extension "pgcrypto";' -psql --host=/tmp -d $POSTGRES_DB -c 'create extension "pg-gvm";' +DBA_EXISTS="$(echo "SELECT 1 FROM pg_roles WHERE rolname = 'dba';" | psql --host=/tmp -d $POSTGRES_DB --tuples-only)" +if [ -z "$DBA_EXISTS" ]; then + psql --host=/tmp -d $POSTGRES_DB -c "create role dba with superuser noinherit;" + psql --host=/tmp -d $POSTGRES_DB -c "grant dba to $POSTGRES_USER;" +fi + +UUID_OSSP_EXTENSION_EXISTS="$(echo "SELECT 1 FROM pg_extension WHERE extname = 'uuid-ossp';" | psql --host=/tmp -d $POSTGRES_DB --tuples-only)" +if [ -z "$UUID_OSSP_EXTENSION_EXISTS" ]; then + psql --host=/tmp -d $POSTGRES_DB -c 'create extension "uuid-ossp";' +fi + +PGCRYPTO_EXTENSION_EXISTS="$(echo "SELECT 1 FROM pg_extension WHERE extname = 'pgcrypto';" | psql --host=/tmp -d $POSTGRES_DB --tuples-only)" +if [ -z "$PGCRYPTO_EXTENSION_EXISTS" ]; then + psql --host=/tmp -d $POSTGRES_DB -c 'create extension "pgcrypto";' +fi + +PG_GVM_EXTENSION_EXISTS="$(echo "SELECT 1 FROM pg_extension WHERE extname = 'pg-gvm';" | psql --host=/tmp -d $POSTGRES_DB --tuples-only)" +if [ -z "$PG_GVM_EXTENSION_EXISTS" ]; then + psql --host=/tmp -d $POSTGRES_DB -c 'create extension "pg-gvm";' +fi pg_ctlcluster --foreground $POSTGRES_VERSION main stop