-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·58 lines (48 loc) · 1.89 KB
/
entrypoint.sh
File metadata and controls
executable file
·58 lines (48 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
export PGHOST="${POSTGRES_HOST}"
export PGUSER="${POSTGRES_SUPER_USER:-postgres}"
export PGPASSWORD="${POSTGRES_SUPER_PASS}"
export PGLOCALE="${POSTGRES_LOCALE:-en_US.utf-8}"
if [[ -z "${PGHOST}" || -z "${PGUSER}" || -z "${PGPASSWORD}" || -z "${POSTGRES_USER}" || -z "${POSTGRES_PASS}" || -z "${POSTGRES_DB}" ]]; then
printf "\e[1;32m%-6s\e[m\n" "Invalid configuration ..."
exit 1
fi
until pg_isready; do
printf "\e[1;32m%-6s\e[m\n" "Waiting for Host '${PGHOST}' ..."
sleep 1
done
if [[ "${POSTGRES_USER_RESET}" == "true" && "${POSTGRES_RESET_CONFIRM}" == "YES" ]]; then
printf "\e[1;32m%-6s\e[m\n" "Drop User ${POSTGRES_USER} ..."
dropuser "${POSTGRES_USER}"
fi
user_exists=$(\
psql \
--tuples-only \
--csv \
--command "SELECT 1 FROM pg_roles WHERE rolname = '${POSTGRES_USER}'"
)
if [[ -z "${user_exists}" ]]; then
printf "\e[1;32m%-6s\e[m\n" "Create User ${POSTGRES_USER} ..."
createuser ${POSTGRES_USER_FLAGS} "${POSTGRES_USER}"
printf "\e[1;32m%-6s\e[m\n" "Update User Password ..."
psql --command "alter user \"${POSTGRES_USER}\" with encrypted password '${POSTGRES_PASS}';"
fi
for init_db in ${POSTGRES_DB}
do
if [[ "${POSTGRES_RESET}" == "YES" && "${POSTGRES_RESET_CONFIRM}" == "YES" ]]; then
printf "\e[1;32m%-6s\e[m\n" "Drop Database ${init_db} ..."
dropdb "${init_db}"
fi
database_exists=$(\
psql \
--tuples-only \
--csv \
--command "SELECT 1 FROM pg_database WHERE datname = '${init_db}'"
)
if [[ -z "${database_exists}" ]]; then
printf "\e[1;32m%-6s\e[m\n" "Create Database ${init_db} ..."
createdb --owner "${POSTGRES_USER}" --locale "${PGLOCALE}" "${init_db}"
fi
printf "\e[1;32m%-6s\e[m\n" "Update User Privileges on Database ..."
psql --command "grant all privileges on database \"${init_db}\" to \"${POSTGRES_USER}\";"
done