#!/bin/bash set -e export POSTGRES_USER="qa_postgres" export POSTGRES_PASSWORD="xM9A4!exJbQNhqJ" export POSTGRES_DB="qa_simfony_billing" export MASTER_PASSWORD="pP5UUJM0mj5b7rZMU5C6" echo "host replication qa_postgres 172.16.178.4/32 trust" >> /var/lib/postgresql/data/pgdata/pg_hba.conf echo "host replication qa_postgres 172.16.178.3/32 trust" >> /var/lib/postgresql/data/pgdata/pg_hba.conf echo "host replication qa_postgres 172.16.178.2/32 trust" >> /var/lib/postgresql/data/pgdata/pg_hba.conf # Create the postgres user if it doesn't exist psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL DO \$do\$ BEGIN IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '$POSTGRES_USER') THEN CREATE ROLE $POSTGRES_USER LOGIN SUPERUSER PASSWORD '$POSTGRES_PASSWORD'; END IF; END \$do\$; EOSQL # # Check if the database is initialized # if [ -s /var/lib/postgresql/data/pgdata/PG_VERSION ]; then # echo 'Initializing database directory'; # # su postgres -c 'initdb -D /var/lib/postgresql/data/pgdata --auth-local=md5 --auth-host=md5 -U postgres -W'; # exec su postgres -c 'echo "$MASTER_PASSWORD" | initdb -D /var/lib/postgresql/data/pgdata --auth-local=md5 --auth-host=md5 -U postgres -W'; # # Copy configuration files # exec su postgres -c 'cp /var/lib/postgresql/data/postgresql.conf /var/lib/postgresql/data/pgdata/postgresql.conf'; # exec su postgres -c 'cp /var/lib/postgresql/data/pg_hba.conf /var/lib/postgresql/data/pgdata/pg_hba.conf'; # exec su postgres -c 'cp /var/lib/postgresql/data/server.crt /var/lib/postgresql/data/pgdata/server.crt'; # exec su postgres -c 'cp /var/lib/postgresql/data/server.key /var/lib/postgresql/data/pgdata/server.key'; # # Start PostgreSQL server to apply configuration # exec su postgres -c 'pg_ctl -D /var/lib/postgresql/data/pgdata -o "-c listen_addresses='*'" -w start' # # Create the database and set up roles # su postgres -c "psql --username \"${POSTGRES_USER}\" <<-EOSQL # CREATE DATABASE ${POSTGRES_DB}; # ALTER USER ${POSTGRES_USER} WITH PASSWORD '${POSTGRES_PASSWORD}'; # EOSQL" # exec su postgres -c 'pg_ctl -D /var/lib/postgresql/data/pgdata -m fast -w stop' # fi; # # Set the ownership of the data directory to the postgres user # chown -R postgres:postgres /var/lib/postgresql/data # # Start PostgreSQL # exec su postgres -c 'pg_ctl -D /var/lib/postgresql/data/pgdata -l /var/lib/postgresql/data/pgdata/logfile start' # # exec su postgres -c 'postgres -c config_file=/var/lib/postgresql/data/pgdata/postgresql.conf'