1
0

create-multiple-postgresql-databases.sh 576 B

123456789101112131415161718192021
  1. set -e
  2. set -u
  3. function create_user_and_database() {
  4. local database=$1
  5. echo " Creating user and database '$database'"
  6. psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
  7. CREATE USER $database;
  8. CREATE DATABASE $database;
  9. GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
  10. EOSQL
  11. }
  12. if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
  13. echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
  14. for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
  15. create_user_and_database $db
  16. done
  17. echo "Multiple databases created"
  18. fi