-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait-for-psql.sh
61 lines (50 loc) · 1.72 KB
/
wait-for-psql.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env bash
# wait-for-psql.sh ------------------------------------------------------------
# Wait for Postgres to become available and execute a command with arguments
# when the server is up.
#------------------------------------------------------------------------------
# config ----------------------------------------------------------------------
sleeptime=1
# functions -------------------------------------------------------------------
function usage()
{
cat << USAGE >&2
Usage:
wait-for-psql.sh TIMEOUT HOST PORT DBNAME USERNAME PASSWORD [COMMAND] [ARGUMENTS...]
TIMEOUT Timeout in seconds
HOST Host or IP of the postgres server
PORT Postgres server port
DBNAME Postgres database name
USERNAME Postgres db user
PASSWORD Postgres db password
COMMAND ARGS Execute command with args after the test finishes
Exit codes: 0 = Postgres available, 1 = timeout.
USAGE
}
# process arguments -----------------------------------------------------------
if [ "$#" -lt 6 ]; then
printf '\nWrong number of arguments passed! At least 5 args are required.'
usage
exit 1
fi
timeout="$1"
host="$2"
port="$3"
dbname="$4"
user="$5"
export PGPASSWORD="$6"
# set command to execute, if psql server is online within timeout -------------
shift 6
cmd="$@"
# test postgres until timeout -------------------------------------------------
until psql -h "$host" -p "$port" -d "$dbname" -U "$user" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping for ${sleeptime}s..."
sleep $sleeptime
if [ "$SECONDS" -gt "$timeout" ]; then
>&2 echo Timeout
exit 1
fi
done
# postgres is up
>&2 echo "Postgres is up - executing command: $cmd"
exec $cmd