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