|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +RECOVERY_FILE="standby.signal" |
| 5 | + |
| 6 | +DATA_DIR=/var/lib/postgresql/data |
| 7 | +DATA_SLAVE_PHYSICAL_DIR=/var/lib/postgresql/data_slave_physical |
| 8 | +WAL_DIR=/var/lib/postgresql/wals |
| 9 | +DATA_SLAVE_LOGICAL_DIR=/var/lib/postgresql/data_slave_logical |
| 10 | + |
| 11 | +su postgres -c '/usr/local/bin/docker-entrypoint.sh postgres "$@" &' |
| 12 | +sleep 5 |
| 13 | +su postgres -c "pg_ctl stop -D $DATA_DIR" |
| 14 | + |
| 15 | +sudo mkdir -p $DATA_SLAVE_PHYSICAL_DIR |
| 16 | +sudo mkdir -p $WAL_DIR |
| 17 | +sudo chown -R postgres:postgres $DATA_SLAVE_PHYSICAL_DIR $WAL_DIR |
| 18 | +sudo chmod 700 $DATA_SLAVE_PHYSICAL_DIR |
| 19 | + |
| 20 | +sudo -u postgres echo "shared_preload_libraries = 'pg_stat_statements'" >> $DATA_DIR/postgresql.conf |
| 21 | +sudo -u postgres echo "pg_stat_statements.track = all" >> $DATA_DIR/postgresql.conf |
| 22 | +sudo -u postgres echo "archive_mode=on" >> $DATA_DIR/postgresql.conf |
| 23 | +sudo -u postgres echo "archive_command='cp %p $WAL_DIR/%f'" >> $DATA_DIR/postgresql.conf |
| 24 | +sudo -u postgres echo "wal_level=replica" >> $DATA_DIR/postgresql.conf |
| 25 | +sudo -u postgres echo "max_wal_senders=4" >> $DATA_DIR/postgresql.conf |
| 26 | +sudo -u postgres echo "hot_standby=on" >> $DATA_DIR/postgresql.conf |
| 27 | + |
| 28 | +sudo -u postgres echo "track_io_timing = on" >> $DATA_DIR/postgresql.conf |
| 29 | +sudo -u postgres echo "track_functions = all" >> $DATA_DIR/postgresql.conf |
| 30 | + |
| 31 | +sudo -u postgres echo "host replication replicator 127.0.0.1/0 trust" >> $DATA_DIR/pg_hba.conf |
| 32 | + |
| 33 | +su postgres -c "pg_ctl start -D $DATA_DIR" |
| 34 | +sleep 3 |
| 35 | + |
| 36 | +sudo -u postgres psql -c "CREATE DATABASE mamonsu_test_db;" |
| 37 | +sudo -u postgres psql -d mamonsu_test_db -c "CREATE EXTENSION pg_stat_statements;" |
| 38 | +sudo -u postgres psql -d mamonsu_test_db -c "CREATE EXTENSION pg_buffercache;" |
| 39 | +sudo -u postgres psql -d mamonsu_test_db -c "CREATE TABLE mamonsu_test_table(id serial, value integer);" |
| 40 | +sudo -u postgres psql -d mamonsu_test_db -c "INSERT INTO mamonsu_test_table(value) SELECT * FROM generate_series(1, 10000);" |
| 41 | +sudo -u postgres psql -c "CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'secret';" |
| 42 | +sudo -u postgres pg_basebackup -h 127.0.0.1 -U replicator -Fp -Xs -P -R -D $DATA_SLAVE_PHYSICAL_DIR/ |
| 43 | +sudo -u postgres sed -i '/^archive_mode/s/^\(.*\)$/#\1/' $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf |
| 44 | +sudo -u postgres sed -i '/^archive_command/s/^\(.*\)$/#\1/' $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf |
| 45 | +sudo -u postgres echo "port=5433" >> $DATA_SLAVE_PHYSICAL_DIR/postgresql.conf |
| 46 | +sudo -u postgres echo "restore_command = 'cp $WAL_DIR/%f %p'" >> $DATA_SLAVE_PHYSICAL_DIR/${RECOVERY_FILE} |
| 47 | + |
| 48 | +su postgres -c "pg_ctl start -D $DATA_SLAVE_PHYSICAL_DIR" |
| 49 | + |
| 50 | +# create logical slave |
| 51 | +if [ "$POSTGRES_VERSION" -ge 100 ]; then # TODO: пофиксить и объединить или убрать вообще |
| 52 | + # create PGDATA directory |
| 53 | + sudo mkdir -p $DATA_SLAVE_LOGICAL_DIR |
| 54 | + sudo chown postgres:postgres $DATA_SLAVE_LOGICAL_DIR |
| 55 | + sudo chmod 700 $DATA_SLAVE_LOGICAL_DIR |
| 56 | + |
| 57 | + sudo -u postgres sed -i '/^wal_level/s/^\(.*\)$/#\1/' $DATA_DIR/postgresql.conf |
| 58 | + sudo -u postgres echo "wal_level=logical" >> $DATA_DIR/postgresql.conf |
| 59 | + su postgres -c "pg_ctl restart -D $DATA_DIR" |
| 60 | + sleep 3 |
| 61 | + sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE mamonsu_test_db TO replicator;" |
| 62 | + sudo -u postgres psql -d mamonsu_test_db -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO replicator;" |
| 63 | + sudo -u postgres psql -d mamonsu_test_db -c "CREATE PUBLICATION mamonsu_publication;" |
| 64 | + sudo -u postgres psql -d mamonsu_test_db -c "ALTER PUBLICATION mamonsu_publication ADD TABLE mamonsu_test_table;" |
| 65 | + sudo -u postgres echo "host all all 127.0.0.1/0 trust" >> $DATA_SLAVE_LOGICAL_DIR/pg_hba.conf |
| 66 | + sudo -u postgres echo "port=5434" >> $DATA_SLAVE_LOGICAL_DIR/postgresql.conf |
| 67 | + su postgres -c "pg_ctl start -D $DATA_SLAVE_LOGICAL_DIR" |
| 68 | + sleep 3 |
| 69 | + sudo -u postgres psql -p 5434 -c "CREATE DATABASE mamonsu_test_db;" |
| 70 | + sudo -u postgres psql -p 5434 -d mamonsu_test_db -c "CREATE TABLE mamonsu_test_table(id serial, value integer);" |
| 71 | + sudo -u postgres psql -p 5434 -d mamonsu_test_db -c "CREATE SUBSCRIPTION mamonsu_subscription CONNECTION 'host=127.0.0.1 port=5432 user=replicator dbname=mamonsu_test_db' PUBLICATION mamonsu_publication;" |
| 72 | +fi |
| 73 | + |
| 74 | +mamonsu bootstrap -x --user postgres -d mamonsu_test_db |
| 75 | +service mamonsu restart |
| 76 | + |
| 77 | +tail -f /dev/null |
0 commit comments