Postgresql 9.0 이상에서 Replication 구성하기

2011. 2. 24. 09:52Database

Postgresql 9.0 이상에서 Replication 구성하기

1. Master postgresql 에서 다음을 수정한다.

* data/postgresql.conf  맨 아래에 수정한다. (디렉토리는 본인에 맞게 수정)

listen_addresses = '*' # to accept a connection from the slave
wal_level = hot_standby     # to generate WAL records for SR purpose.
archive_mode = on           # to enable the archiving log mode.
archive_command = 'cp %p home/app/postgresql-9.0.3.1/data/pgsql/pg_xlog_archive/%f'   # to specify the log archiving command.
max_wal_senders = 5         # to specify the max number of the slave(s).
wal_keep_segments = 32      # to specify the number of the previous WAL files to hold on the master.

* data/pg_hba.conf 에 아래를 추가한다. (ip는 Slave postgresql 로 한다.)

host   replication    all    192.168.0.202/32        trust


2. 폴더를 만든다. (postgresql.conf에서 지정한 폴더)

   home/app/postgresql-9.0.3.1/data/pgsql/pg_xlog_archive

3. Master db에서 data 폴더를 백업한다.

psql> select pg_start_backup('initial backup for SR');   <=psql 콘솔에서 실행
#> tar cvf pg_backup.tar  /home/app/postgresql-9.0.3.1/data  <=일반 콘솔에서 실행
psql> selectT pg_stop_backup(); <=psql 콘솔에서 실행

* 참고로 Master/Slave는 같은 시스템이어야 한다.(예> 동일한 linux 64bit)

4. 백업된 파일을 slave pc에 복사한다.

5. Slave의 data폴더를 삭제하고 백업된 해당 data 폴더에 푼다.
   (즉 Master의 data폴더를 그대로 붙여넣는다는 얘기다)

6. Slave/data 폴더에서 postmaster.pid를 삭제한다.

6. Slave postgresql 에서 다음을 설정한다.

* data/postgresql.conf
hot_standby = on # read only

* recovery.conf ( 이건 파일을 생성해야한다.)
standby_mode = 'on'      # to enable the standby (read-only) mode.
primary_conninfo = 'host=192.168.0.203 port=5432 user=postgres'     # 마스터 ip를 지정한다.
trigger_file = '/tmp/pg_failover_trigger'            # to specify a trigger file to recognize a fail over.
restore_command = 'cp /home/app/postgresql-9.0.3.1/data/pgsql/pg_xlog_archive/%f "%p"'

7. Master를 시작한다. Slave를 시작한다.

 service postgresql-9.0 start

8. 이제 Admin Tool or psql 콘솔에서 테스트를 해보면 된다.


* 기타
우분투 에서 아래와 같이 나올경우

 postgres@debian:~$ /opt/PostgreSQL/9.0/bin/psql
 Password: psql (9.0.2) Type "help" for help.
 Cannot read termcap database;
using dumb terminal settings.
 Aborted

*termcap를 설치해야한다.

1. termcap1.3.1을 받는다.
2. 압축을 푼다
3. ./configure --enable-install-termcap
4. make
5. make install
6. pssql 을 실행한다.