diff --git a/docker-compose.yml b/docker-compose.yml index 7dec4fd..93703e1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,9 @@ services: - "${DB_PORT:-5432}:5432" volumes: - postgres_data:/var/lib/postgresql/data + - ./postgres-entrypoint.sh:/usr/local/bin/postgres-entrypoint.sh:ro + entrypoint: ["/usr/local/bin/postgres-entrypoint.sh"] + command: ["postgres"] healthcheck: test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-jobsboard}"] interval: 5s diff --git a/postgres-entrypoint.sh b/postgres-entrypoint.sh new file mode 100755 index 0000000..80e562d --- /dev/null +++ b/postgres-entrypoint.sh @@ -0,0 +1,28 @@ +#!/bin/sh +set -e + +# Run background synchronization once PostgreSQL is up on local unix socket +sync_credentials() { + until pg_isready -q -h /var/run/postgresql; do + sleep 0.5 + done + + USER="${POSTGRES_USER:-postgres}" + PASS="${POSTGRES_PASSWORD:-postgres}" + + psql -v ON_ERROR_STOP=0 -h /var/run/postgresql -U postgres -d postgres <<-EOSQL >/dev/null 2>&1 + DO \$\$ + BEGIN + IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '$USER') THEN + CREATE ROLE "$USER" WITH LOGIN SUPERUSER PASSWORD '$PASS'; + ELSE + ALTER ROLE "$USER" WITH LOGIN SUPERUSER PASSWORD '$PASS'; + END IF; + END + \$\$; +EOSQL +} + +sync_credentials & + +exec docker-entrypoint.sh "$@"