|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# wtf is that? |
| 5 | +if [ "${1:0:1}" = '-' ]; then |
| 6 | + set -- postgres "$@" |
| 7 | +fi |
| 8 | + |
| 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 |
| 59 | + |
| 60 | + ############################################################################ |
| 61 | + |
| 62 | + CONNSTRS='dbname=postgres host=node1, dbname=postgres host=node2, dbname=postgres host=node3' |
| 63 | + RAFT_PEERS='1:172.18.0.2:6666, 2:172.18.0.4:6666, 3:172.18.0.3:6666' |
| 64 | + |
| 65 | + cat <<-EOF >> $PGDATA/postgresql.conf |
| 66 | + listen_addresses='*' |
| 67 | + max_prepared_transactions = 100 |
| 68 | + synchronous_commit = off |
| 69 | + wal_level = logical |
| 70 | + max_worker_processes = 15 |
| 71 | + max_replication_slots = 10 |
| 72 | + max_wal_senders = 10 |
| 73 | + shared_preload_libraries = 'raftable,multimaster' |
| 74 | + raftable.id = $NODE_ID |
| 75 | + raftable.peers = '$RAFT_PEERS' |
| 76 | + multimaster.workers=4 |
| 77 | + multimaster.use_raftable=true |
| 78 | + multimaster.queue_size=52857600 |
| 79 | + multimaster.ignore_tables_without_pk=1 |
| 80 | + multimaster.node_id = $NODE_ID |
| 81 | + multimaster.conn_strings = '$CONNSTRS' |
| 82 | + EOF |
| 83 | + |
| 84 | + tail -n 20 $PGDATA/postgresql.conf |
| 85 | + |
| 86 | + pg_ctl -D "$PGDATA" -m fast -w stop |
| 87 | + |
| 88 | + echo |
| 89 | + echo 'PostgreSQL init process complete; ready for start up.' |
| 90 | + echo |
| 91 | + fi |
| 92 | +fi |
| 93 | + |
| 94 | +exec "$@" |
| 95 | + |
0 commit comments