Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #!/bin/bash
  2. set -e
  3. set -x
  4.  
  5. if [ "${1:0:1}" = '-' ]; then
  6. set -- postgres "$@"
  7. fi
  8.  
  9. if [ "$1" = 'postgres' ]; then
  10. mkdir -p "$PGDATA"
  11. chmod 700 "$PGDATA"
  12. chown -R postgres "$PGDATA"
  13.  
  14. chmod g+s /run/postgresql
  15. chown -R postgres /run/postgresql
  16.  
  17. # look specifically for PG_VERSION, as it is expected in the DB dir
  18. if [ ! -s "$PGDATA/PG_VERSION" ]; then
  19. eval "gosu postgres initdb $POSTGRES_INITDB_ARGS"
  20.  
  21. # check password first so we can output the warning before postgres
  22. # messes it up
  23. if [ "$POSTGRES_PASSWORD" ]; then
  24. pass="PASSWORD '$POSTGRES_PASSWORD'"
  25. authMethod=md5
  26. else
  27. # The - option suppresses leading tabs but *not* spaces. :)
  28. cat >&2 <<-'EOWARN'
  29. ****************************************************
  30. WARNING: No password has been set for the database.
  31. This will allow anyone with access to the
  32. Postgres port to access your database. In
  33. Docker's default configuration, this is
  34. effectively any other container on the same
  35. system.
  36.  
  37. Use "-e POSTGRES_PASSWORD=password" to set
  38. it in "docker run".
  39. ****************************************************
  40. EOWARN
  41.  
  42. pass=
  43. authMethod=trust
  44. fi
  45.  
  46. { echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
  47.  
  48. # internal start of server in order to allow set-up using psql-client
  49. # does not listen on external TCP/IP and waits until start finishes
  50. gosu postgres pg_ctl -D "$PGDATA" \
  51. -o "-c listen_addresses='localhost'" \
  52. -w start
  53.  
  54. : ${POSTGRES_USER:=postgres}
  55. : ${POSTGRES_DB:=$POSTGRES_USER}
  56. export POSTGRES_USER POSTGRES_DB
  57.  
  58. psql=( psql -v ON_ERROR_STOP=1 )
  59.  
  60. if [ "$POSTGRES_DB" != 'postgres' ]; then
  61. "${psql[@]}" --username postgres <<-EOSQL
  62. CREATE DATABASE "$POSTGRES_DB" ;
  63. EOSQL
  64. echo
  65. fi
  66.  
  67. if [ "$POSTGRES_USER" = 'postgres' ]; then
  68. op='ALTER'
  69. else
  70. op='CREATE'
  71. fi
  72. "${psql[@]}" --username postgres <<-EOSQL
  73. $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
  74. EOSQL
  75. echo
  76.  
  77. psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
  78.  
  79. echo
  80. for f in /docker-entrypoint-initdb.d/*; do
  81. case "$f" in
  82. *.sh) echo "$0: running $f"; . "$f" ;;
  83. *.sql) echo "$0: running $f"; "${psql[@]}" < "$f"; echo ;;
  84. *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
  85. *) echo "$0: ignoring $f" ;;
  86. esac
  87. echo
  88. done
  89.  
  90. gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
  91.  
  92. echo
  93. echo 'PostgreSQL init process complete; ready for start up.'
  94. echo
  95. else
  96. # Perform any maintenance activity
  97.  
  98. gosu postgres pg_ctl -D "$PGDATA" \
  99. -o "-c listen_addresses='localhost'" \
  100. -w start
  101.  
  102. echo
  103. for f in /docker-entrypoint-maintaindb.d/*; do
  104. case "$f" in
  105. *.sh) echo "$0: running $f"; . "$f" ;;
  106. *.sql) echo "$0: running $f"; "${psql[@]}" < "$f"; echo ;;
  107. *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
  108. *) echo "$0: ignoring $f" ;;
  109. esac
  110. echo
  111. done
  112.  
  113.  
  114. gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
  115. fi
  116.  
  117. exec gosu postgres "$@"
  118. fi
  119.  
  120. exec "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement