replication 자동구성 스크립트(테스트 only)

POSTGRESQL 2018. 12. 4. 14:35

local 테스트 환경에서 replication 구성 스크립트 입니다.

#!/bin/sh

set -e

export PG_BIN_DIR="/usr/local/pgsql/bin"
export PG_DIR="$PWD"

export PRIMARY_PORT=5444
export REPLICA_PORT=5445

read -p "Will delete $PG_DIR/data/{primary,replica}. Okay? [Ctrl+C cancels]" yn
rm -rf $PG_DIR/data/primary
rm -rf $PG_DIR/data/replica

# Initialize a new data directory for the primary, then use a bit of a shortcut
# by just copying it for use by the replica.
$PG_BIN_DIR/initdb -D $PG_DIR/data/primary/
cp -r $PG_DIR/data/primary/ $PG_DIR/data/replica/

cat << EOT >> $PG_DIR/data/primary/postgresql.conf
port=$PRIMARY_PORT
EOT

cat << EOT >> $PG_DIR/data/replica/postgresql.conf
port=$REPLICA_PORT
shared_buffers=500MB
hot_standby=on
hot_standby_feedback=on
EOT

cat << EOT >> $PG_DIR/data/replica/recovery.conf
standby_mode=on
primary_conninfo='host=127.0.0.1 port=$PRIMARY_PORT user=$USER'
EOT

cat << EOT >> /dev/stdout
READY!
======

Start primary:
    $PG_BIN_DIR/postgres -D $PG_DIR/data/primary

Start replica:
    $PG_BIN_DIR/postgres -D $PG_DIR/data/replica

Create a database:
    $PG_BIN_DIR/createdb -p $PRIMARY_PORT mydb

Connect to primary:
    $PG_BIN_DIR/psql -p $PRIMARY_PORT mydb

Connect to replica:
    $PG_BIN_DIR/psql -p $REPLICA_PORT mydb
EOT