fix(db): add postgres auto-sync entrypoint wrapper to ensure role login and credentials persist across rebuilds
This commit is contained in:
parent
0aba1993f8
commit
c7259411ae
2 changed files with 31 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
28
postgres-entrypoint.sh
Executable file
28
postgres-entrypoint.sh
Executable file
|
|
@ -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 "$@"
|
||||
Loading…
Add table
Reference in a new issue