-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-postgres.sh
executable file
·34 lines (27 loc) · 1009 Bytes
/
init-postgres.sh
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
#!/bin/bash
set -e;
chmod +x /docker-entrypoint-initdb.d/init-data.sh
INIT_DB_SQL="/docker-entrypoint-initdb.d/init-db.sql"
if [ -n "${DB_USER}" ] && [ -n "${DB_DATABASE}" ]; then
until pg_isready --quiet; do
echo "Waiting PostgreSQL running..."
sleep 1
done
# Check existing db
if [ -z "$(psql -qtAX --dbname="${DB_DATABASE}" -c "SELECT datname FROM pg_database WHERE datname='${DB_DATABASE}'")" ]; then
echo "Creating database: ${DB_DATABASE}"
createdb -E UTF8 "${DB_DATABASE}"
fi
# Check existing user
if [ -z "$(psql -qtAX --dbname=postgres -c "SELECT usename FROM pg_user WHERE usename='$DB_USER'")" ]; then
echo "Creating user: '$DB_USER'"
createuser --createdb --login --password "$DB_USER"
fi
# Init db scripts if its exist
if [ -f "$INIT_DB_SQL" ]; then
echo "Initializing database"
psql -v ON_ERROR_STOP=1 --username "$DB_USER" --dbname "DB_DATABASE" < "$INIT_DB_SQL"
fi
else
echo "SETUP INFO: No Environment variables given!"
fi