|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -Eeo pipefail |
| 3 | + |
| 4 | +source /usr/local/bin/docker-entrypoint.sh |
| 5 | + |
| 6 | +# sync $POSTGRES_PASSWORD to supabase-specific roles |
| 7 | +pg_sync_password() { |
| 8 | + # Wait for PostgreSQL to be ready with timeout |
| 9 | + timeout 30s bash -c 'until pg_isready -h localhost >/dev/null 2>&1; do |
| 10 | + echo "Waiting for PostgreSQL to be ready..." |
| 11 | + sleep 1 |
| 12 | + done' || { |
| 13 | + echo "Timeout waiting for PostgreSQL to be ready after 30 seconds" |
| 14 | + return 1 |
| 15 | + } |
| 16 | + |
| 17 | + # Connect via TCP/IP to localhost and alter the supabase_admin password |
| 18 | + psql -h localhost -U supabase_admin <<-'EOSQL' |
| 19 | + \set pgpass `echo "$POSTGRES_PASSWORD"` |
| 20 | + ALTER USER supabase_admin WITH PASSWORD :'pgpass'; |
| 21 | +EOSQL |
| 22 | + |
| 23 | + # execute the roles SQL file using psql |
| 24 | + psql -h localhost -U supabase_admin -f "/docker-entrypoint-initdb.d/init-scripts/99-roles.sql" |
| 25 | +} |
| 26 | + |
| 27 | +_main() { |
| 28 | + # if first arg looks like a flag, assume we want to run postgres server |
| 29 | + if [ "${1:0:1}" = '-' ]; then |
| 30 | + set -- postgres "$@" |
| 31 | + fi |
| 32 | + |
| 33 | + if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then |
| 34 | + docker_setup_env |
| 35 | + # setup data directories and permissions (when run as root) |
| 36 | + docker_create_db_directories |
| 37 | + if [ "$(id -u)" = '0' ]; then |
| 38 | + # then restart script as postgres user |
| 39 | + exec gosu postgres "$BASH_SOURCE" "$@" |
| 40 | + fi |
| 41 | + |
| 42 | + # only run initialization on an empty data directory |
| 43 | + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then |
| 44 | + docker_verify_minimum_env |
| 45 | + |
| 46 | + # check dir permissions to reduce likelihood of half-initialized database |
| 47 | + ls /docker-entrypoint-initdb.d/ > /dev/null |
| 48 | + |
| 49 | + docker_init_database_dir |
| 50 | + pg_setup_hba_conf "$@" |
| 51 | + |
| 52 | + docker_setup_db |
| 53 | + docker_process_init_files /docker-entrypoint-initdb.d/* |
| 54 | + |
| 55 | + cat <<-'EOM' |
| 56 | +
|
| 57 | + PostgreSQL init process complete; ready for start up. |
| 58 | +
|
| 59 | + EOM |
| 60 | + else |
| 61 | + cat <<-'EOM' |
| 62 | +
|
| 63 | + PostgreSQL Database directory appears to contain a database; Skipping initialization |
| 64 | +
|
| 65 | + EOM |
| 66 | + fi |
| 67 | + |
| 68 | + # Start pg_sync_password in the background |
| 69 | + pg_sync_password "$@" & |
| 70 | + fi |
| 71 | + |
| 72 | + # Execute the main postgres command |
| 73 | + exec "$@" |
| 74 | +} |
| 75 | + |
| 76 | +if ! _is_sourced; then |
| 77 | + _main "$@" |
| 78 | +fi |
0 commit comments