Skip to content

Commit

Permalink
feat(postgres): create additional databases at startup (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli authored Dec 18, 2024
1 parent 2446c67 commit 71e3918
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/development/stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ services:
# You can access the database via `adminer`.
command: -c maevsi.jwt_expiry_duration='1 month'
environment:
POSTGRES_ADDITIONAL_DBS: grafana
POSTGRES_DB_FILE: /run/secrets/postgres_db
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
POSTGRES_USER_FILE: /run/secrets/postgres_user
Expand All @@ -324,7 +325,8 @@ services:
# - net.ipv4.tcp_keepalive_probes=10
volumes:
- /run/:/run/ # Make PGSQL socket available. # #DARGSTACK-REMOVE
- postgres_data:/var/lib/postgresql/data
- postgres_data:/var/lib/postgresql/data/
- ../production/configurations/postgres/docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d/:ro
prometheus:
# You can access the metrics monitoring at [prometheus.localhost](https://prometheus.localhost/).
deploy:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure this directory (!) and all files within it have permission 755.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

set -eu

username="$(cat /run/secrets/postgres_user)"

create_database() {
create_database_database=$1
echo "Creating user and database '$create_database_database'"
psql -v ON_ERROR_STOP=1 --username "$username" --dbname "postgres" <<-EOSQL
CREATE DATABASE "$create_database_database";
EOSQL
}

if [ -n "${POSTGRES_ADDITIONAL_DBS:-}" ]; then
echo "Additional database creation requested: $POSTGRES_ADDITIONAL_DBS"

for db in $POSTGRES_ADDITIONAL_DBS; do
create_database "$db"
done

echo "Multiple databases created"
fi

0 comments on commit 71e3918

Please sign in to comment.