forked from VyacheslavMik/aidboxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry-point.sh
133 lines (98 loc) · 3.51 KB
/
entry-point.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
set -e
set -o xtrace
export LD_LIBRARY_PATH=/pg/lib
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
PATH=/pg/bin/:$PATH
if [ "${1:0:1}" = '-' ]; then
set -- postgres "$@"
fi
PGDATA=/data
NUM_ATTEMPTS=20
NODE_NAME=$PG_REPLICA
if [ ! -s "/data/PG_VERSION" ]; then
if [ "$PG_ROLE" = 'replica' ]; then
echo "$PG_MASTER_HOST:5432:*:postgres:$POSTGRES_PASSWORD" > ~/.pgpass
chmod 0600 ~/.pgpass
n=0
until [ $n -ge $NUM_ATTEMPTS ]
do
pg_basebackup -D /data -Fp -U postgres -w -R -Xs -c fast -l 'clone' -P -v -h $PG_MASTER_HOST -U postgres && export RESTORED=1 && break
n=$[$n+1]
echo "Not ready; Sleep $n"
sleep $n
done
psql -h $PG_MASTER_HOST -U postgres -w -c "SELECT pg_create_physical_replication_slot('$NODE_NAME');" || echo "may be exists"
echo "restored: $RESTORED"
echo 'hot_standby = on' >> /data/postgresql.conf
echo 'port = 5432' >> /data/postgresql.conf
echo "primary_slot_name = '$NODE_NAME'" >> /data/recovery.conf
echo "standby_mode = 'on'" >> /data/recovery.conf
echo
echo 'PostgreSQL clone process complete; ready for start up.'
echo
else
mkdir -p /data
chmod 700 /data
chown -R postgres /data
file_env 'POSTGRES_INITDB_ARGS'
su - postgres -c "export LD_LIBRARY_PATH=/pg/lib && /pg/bin/initdb --data-checksums -E 'UTF-8' --lc-collate='en_US.UTF-8' --lc-ctype='en_US.UTF-8' -D /data"
# check password first so we can output the warning before postgres
# messes it up
file_env 'POSTGRES_PASSWORD'
pass="PASSWORD '$POSTGRES_PASSWORD'"
authMethod=md5
{ echo; echo "host all all all $authMethod"; } | tee -a "$PGDATA/pg_hba.conf" > /dev/null
{ echo; echo "host replication postgres 0.0.0.0/0 $authMethod"; } | tee -a "$PGDATA/pg_hba.conf" > /dev/null
{ echo; echo "listen_addresses = '*'"; } | tee -a "$PGDATA/postgresql.conf" > /dev/null
su - postgres -c 'export LD_LIBRARY_PATH=/pg/lib && /pg/bin/pg_ctl -D /data -w start'
su - postgres -c 'export LD_LIBRARY_PATH=/pg/lib && /pg/bin/createuser -s root'
echo "ALTER USER postgres WITH SUPERUSER $pass" | /pg/bin/psql postgres
if [ -n "$POSTGRES_DB" ] && [ "$POSTGRES_DB" != 'postgres']; then
/pg/bin/psql postgres -c "create database $POSTGRES_DB"
fi
# shared_preload_libraries='pg_pathman'
# Some tweaks to default configuration
cat <<-CONF >> /data/postgresql.conf
listen_addresses = '*'
synchronous_commit = off
shared_buffers = '2GB'
wal_log_hints = on
wal_level = logical
max_wal_senders = 30
max_replication_slots = 30
max_wal_size = '4GB'
CONF
chown postgres:postgres /data/postgresql.conf
su - postgres -c 'export LD_LIBRARY_PATH=/pg/lib && /pg/bin/pg_ctl -D /data -m fast -w stop'
echo
echo 'PostgreSQL init process complete; ready for start up.'
echo
fi
fi
# allow the container to be started with `--user`
if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
mkdir -p /data
chown -R postgres /data
chmod 700 /data
echo "postgres & 0"
exec gosu postgres "$@"
else
exec "$@"
fi