|
1 | 1 | #!/bin/bash
|
2 | 2 | set -e
|
3 | 3 |
|
4 |
| -# wtf is that? |
5 |
| -if ["${1:0:1}"='-' ];then |
6 |
| -set -- postgres"$@" |
7 |
| -fi |
| 4 | +mkdir -p"$PGDATA" |
| 5 | +chmod 700"$PGDATA" |
| 6 | +chown -R postgres"$PGDATA" |
| 7 | + |
| 8 | +# look specifically for PG_VERSION, as it is expected in the DB dir |
| 9 | +if [!-s"$PGDATA/PG_VERSION" ];then |
| 10 | +initdb |
| 11 | + |
| 12 | +if ["$POSTGRES_PASSWORD" ];then |
| 13 | +pass="PASSWORD '$POSTGRES_PASSWORD'" |
| 14 | +authMethod=md5 |
| 15 | +else |
| 16 | +pass= |
| 17 | +authMethod=trust |
| 18 | +fi |
8 | 19 |
|
9 |
| -if ["$1"='postgres' ];then |
10 |
| -mkdir -p"$PGDATA" |
11 |
| -chmod 700"$PGDATA" |
12 |
| -chown -R postgres"$PGDATA" |
13 |
| - |
14 |
| -# look specifically for PG_VERSION, as it is expected in the DB dir |
15 |
| -if [!-s"$PGDATA/PG_VERSION" ];then |
16 |
| -initdb |
17 |
| - |
18 |
| -if ["$POSTGRES_PASSWORD" ];then |
19 |
| -pass="PASSWORD '$POSTGRES_PASSWORD'" |
20 |
| -authMethod=md5 |
21 |
| -else |
22 |
| -pass= |
23 |
| -authMethod=trust |
24 |
| -fi |
25 |
| - |
26 |
| -{echo;echo"host all all 0.0.0.0/0$authMethod"; }>>"$PGDATA/pg_hba.conf" |
27 |
| -{echo;echo"host replication all 0.0.0.0/0$authMethod"; }>>"$PGDATA/pg_hba.conf" |
28 |
| - |
29 |
| -############################################################################ |
30 |
| - |
31 |
| -# internal start of server in order to allow set-up using psql-client |
32 |
| -# does not listen on TCP/IP and waits until start finishes |
33 |
| -pg_ctl -D"$PGDATA" \ |
34 |
| --o"-c listen_addresses=''" \ |
35 |
| --w start |
36 |
| - |
37 |
| -:${POSTGRES_USER:=postgres} |
38 |
| -:${POSTGRES_DB:=$POSTGRES_USER} |
39 |
| -export POSTGRES_USER POSTGRES_DB |
40 |
| - |
41 |
| -psql=( psql -v ON_ERROR_STOP=1 ) |
42 |
| - |
43 |
| -if ["$POSTGRES_DB"!='postgres' ];then |
44 |
| -"${psql[@]}" --username postgres<<-EOSQL |
45 |
| -CREATE DATABASE "$POSTGRES_DB" ; |
46 |
| -EOSQL |
47 |
| -echo |
48 |
| -fi |
49 |
| - |
50 |
| -if ["$POSTGRES_USER"='postgres' ];then |
51 |
| -op='ALTER' |
52 |
| -else |
53 |
| -op='CREATE' |
54 |
| -fi |
55 |
| -"${psql[@]}" --username postgres<<-EOSQL |
56 |
| -$op USER "$POSTGRES_USER" WITH SUPERUSER$pass ; |
57 |
| -EOSQL |
58 |
| -echo |
| 20 | +{echo;echo"host all all 0.0.0.0/0$authMethod"; }>>"$PGDATA/pg_hba.conf" |
| 21 | +{echo;echo"host replication all 0.0.0.0/0$authMethod"; }>>"$PGDATA/pg_hba.conf" |
59 | 22 |
|
60 |
| -############################################################################ |
61 |
| - |
62 |
| -RAFT_PEERS='1:172.18.0.2:6666, 2:172.18.0.4:6666, 3:172.18.0.3:6666' |
| 23 | +############################################################################ |
63 | 24 |
|
64 |
| -cat<<-EOF >>$PGDATA/postgresql.conf |
65 |
| -listen_addresses='*' |
66 |
| -max_prepared_transactions = 100 |
67 |
| -synchronous_commit = off |
68 |
| -wal_level = logical |
69 |
| -max_worker_processes = 15 |
70 |
| -max_replication_slots = 10 |
71 |
| -max_wal_senders = 10 |
72 |
| -shared_preload_libraries = 'raftable' |
73 |
| -raftable.id =$NODE_ID |
74 |
| -raftable.peers = '$RAFT_PEERS' |
75 |
| -EOF |
| 25 | +# internal start of server in order to allow set-up using psql-client |
| 26 | +# does not listen on TCP/IP and waits until start finishes |
| 27 | +pg_ctl -D"$PGDATA" \ |
| 28 | +-o"-c listen_addresses=''" \ |
| 29 | +-w start |
76 | 30 |
|
77 |
| -tail -n 20$PGDATA/postgresql.conf |
| 31 | +:${POSTGRES_USER:=postgres} |
| 32 | +:${POSTGRES_DB:=$POSTGRES_USER} |
| 33 | +export POSTGRES_USER POSTGRES_DB |
78 | 34 |
|
79 |
| -pg_ctl -D"$PGDATA" -m fast -w stop |
| 35 | +psql=( psql -v ON_ERROR_STOP=1 ) |
80 | 36 |
|
| 37 | +if ["$POSTGRES_DB"!='postgres' ];then |
| 38 | +"${psql[@]}" --username postgres<<-EOSQL |
| 39 | +CREATE DATABASE "$POSTGRES_DB" ; |
| 40 | +EOSQL |
81 | 41 | echo
|
82 |
| -echo'PostgreSQL init process complete; ready for start up.' |
83 |
| -echo |
84 | 42 | fi
|
| 43 | + |
| 44 | +if ["$POSTGRES_USER"='postgres' ];then |
| 45 | +op='ALTER' |
| 46 | +else |
| 47 | +op='CREATE' |
| 48 | +fi |
| 49 | +"${psql[@]}" --username postgres<<-EOSQL |
| 50 | +$op USER "$POSTGRES_USER" WITH SUPERUSER$pass ; |
| 51 | +EOSQL |
| 52 | +echo |
| 53 | + |
| 54 | +############################################################################ |
| 55 | + |
| 56 | +RAFT_PEERS='1:172.18.0.2:6666, 2:172.18.0.4:6666, 3:172.18.0.3:6666' |
| 57 | + |
| 58 | +cat<<-EOF >>$PGDATA/postgresql.conf |
| 59 | +listen_addresses='*' |
| 60 | +max_prepared_transactions = 100 |
| 61 | +synchronous_commit = off |
| 62 | +wal_level = logical |
| 63 | +max_worker_processes = 15 |
| 64 | +max_replication_slots = 10 |
| 65 | +max_wal_senders = 10 |
| 66 | +shared_preload_libraries = 'raftable' |
| 67 | +raftable.id =$NODE_ID |
| 68 | +raftable.peers = '$RAFT_PEERS' |
| 69 | +EOF |
| 70 | + |
| 71 | +tail -n 20$PGDATA/postgresql.conf |
| 72 | + |
| 73 | +pg_ctl -D"$PGDATA" -m fast -w stop |
| 74 | + |
| 75 | +echo |
| 76 | +echo'PostgreSQL init process complete; ready for start up.' |
| 77 | +echo |
85 | 78 | fi
|
86 | 79 |
|
87 | 80 | exec"$@"
|
|