bitnami-create-extra-users.sh 1.39 KB
#!/bin/bash


. /opt/bitnami/scripts/libpostgresql.sh

set -x
ls -al /var/postgresql-config /var/postgresql-secret
declare -i user_index=0 db_index=0
while :; do
	user_name="$(< /var/postgresql-config/POSTGRESQL_USER_${user_index}_NAME)"
	user_password="$(< /var/postgresql-secret/POSTGRESQL_USER_${user_index}_PASSWORD)"
	if [[ -z $user_name ]]; then
		break
	fi
	postgresql_ensure_user_exists "$user_name" --password "$user_password"
	user_index=$((user_index + 1))
	if [[ ! -e /var/postgresql-config/POSTGRESQL_USER_${user_index}_NAME ]]; then
		break
	fi
done
first_user=""
while :; do
	database_name="$(< /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_NAME)"
	postgresql_ensure_database_exists "$database_name"
	user_index=0
	while :; do
		user_name="$(< /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_USER_${user_index})"
		if [[ -z $user_name ]]; then
			break
		elif [[ -z $first_user ]]; then
			first_user="$user_name"
		else
			postgresql_ensure_user_has_database_privileges "$user_name" "$database_name"
		fi
		user_index=$((user_index + 1))
		if [[ ! -e /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_USER_${user_index} ]]; then
			break
		fi
	done
	if [[ $first_user ]]; then
		postgresql_ensure_user_has_database_privileges "$first_user" "$database_name"
	fi
	db_index=$((db_index + 1))
	if [[ ! -e /var/postgresql-config/POSTGRESQL_DATABASE_${db_index}_NAME ]]; then
		break
	fi
done