🔍 들어가며
PostgreSQL 17 버전 기준으로 고가용성(HA) 환경을 구축하는 방법에 대해 정리해보았습니다.
Streaming Replication 기반으로 Active-Standby(액티브-스탠바이) 구조로구성해 보겠습니다.
📚 설치 및 설정 순서
🔧설치 환경
| 데이터베이스 | 운영체제(OS) | Public IP | Private IP | Hostname | |
| Primary Server | PostgreSQL 17 | Oracle Linux 9 (OCI) | 134.185.102.159 | 10.0.0.61 | postdb1 |
| Standby Server | PostgreSQL 17 | Oracle Linux 9 (OCI) | 146.56.115.180 | 10.0.0.117 | postdb2 |
1️⃣ PostgreSQL 설치 전 사전설정(OS Level)
1. /etc/hosts 설정 (Primary, Standby)
$ sudo vi /etc/hosts
127.0.0.1 localhost
# Primary DB IP
#134.185.102.159 postdb1
10.0.0.61 postdb1
# Standby DB IP
#146.56.115.180 postdb2
10.0.0.117 postdb2
2. 필수 패키지 설치 (Primary, Standby)
$ sudo yum -y install gcc gcc-c++ make autoconf readline readline-devel zlib zlib-devel openssl openssl-devel gettext-devel python python-devel perl
$ sudo yum groupinstall "Development Tools" -y
$ sudo yum install -y \
readline readline-devel \
zlib zlib-devel \
openssl openssl-devel \
libicu libicu-devel \
bison flex \
perl perl-devel \
python3 python3-devel \
gettext gettext-devel
3. 방화벽 설정 (Primary, Standby)
$ sudo firewall-cmd --permanent --add-port=5432/tcp
$ sudo firewall-cmd --reload
4. Selinux 설정 (Primary, Standby)
- SELinux는 replication 시 파일 접근을 막을 수 있어서, 실무에서는 꺼두는 경우가 많습니다.
$ sudo vi /etc/selinux/config
...
SELINUX=disabled
...
$ sudo setenforce 0
5. Resource Limit 설정 (Private, Standby)
$ sudo vi /etc/security/limits.conf
#<domain> <type> <item> <value>
#
#* soft core 0
#* hard rss 10000
#@student hard nproc 20
#@faculty soft nproc 20
#@faculty hard nproc 50
#ftp hard nproc 0
#@student - maxlogins 4
postgres soft nofile 1024
postgres hard nofile 65536
# End of file
6. 커널 파라미터 값 설정 및 적용 (Primary, Standby)
$ sudo vi /etc/sysctl.conf
fs.file-max = 6815744
kernel.sem = 250 32000 100 128
kernel.shmmni = 4096
kernel.shmall = 1073741824
kernel.shmmax = 4398046511104
kernel.panic_on_oops = 1
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.default.rp_filter = 2
fs.aio-max-nr = 1048576
net.ipv4.ip_local_port_range = 9000 65500
$ sudo sysctl -p
7. 재부팅(Primary, Standby)
$ sudo reboot
8. 그룹 및 계정 생성 (Primary, Standby)
$ sudo groupadd dba
$ sudo useradd -g dba -G dba postgres
$ sudo passwd postgres
9. 설치할 디렉토리 경로 생성 및 권한 부여 (Primary, Standby)
$ sudo mkdir -p /postgres/app/postgres/pgsql15
$ sudo chown -R postgres:dba /postgres
$ sudo chmod -R 755 /postgres
10. postgres 계정 환경변수 설정 (Primary, Standby)
- 프롬프트 '$'과 '#'(postgres 유저) 구분 주의
$ su - postgres
# vi ~/.bash_profile
export PG_HOME=/postgres/app/postgres/pgsql17
export PGDATA=$PG_HOME/data
export PGLIB=$PG_HOME/lib
export PATH=$PG_HOME/bin:$PATH
export LD_LIBRARY_PATH=/postgres/app/postgres/lib
# source ~/.bash_profile
2️⃣ PostgreSQL 17 설치 및 설정
1. PostgreSQL 17 설치 (Primary, Standby)
- 아래 링크에서 파일 다운로드 후 서버에 업로드
파일설치: https://www.postgresql.org/ftp/source/v17.5/
PostgreSQL: File Browser
www.postgresql.org
- tar.gz 압축풀기
# cd /home/postgres/
# tar -zxvf postgresql-17.5.tar.gz
- postgresql 소스빌드 전 환경 설정 준비 및 설치
# cd postgresql-17.5
# ./configure --prefix=/postgres/app/postgres/pgsql17 --enable-depend --enable-nls=utf-8 --with-python
# make
# make check
# make install
2. database 생성 (Primary, Standby)
# cd /postgres/app/postgres/pgsql17/bin
# initdb -E utf-8 --locale=en_US.UTF-8 -D /postgres/app/postgres/pgsql17/data
3. Archiv WAL File 저장 디렉토리, DB Log 디렉토리 경로 생성 (Primary, Standby)
# cd $PG_HOME
# mkdir pg_log
# mkdir pg_arch
4. PostgreSQL DB 파라미터 설정(Primary)
- postgresql.conf 파일에 작성
# cd $PGDATA
# vi postgresql.conf
..(생략)..
#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------
cluster_name = 'primary_db'
listen_addresses = '10.0.0.61'
port = 5432
max_connections = 100
superuser_reserved_connections = 10
shared_buffers = 3GB
temp_buffers = 8MB
work_mem = 8MB
maintenance_work_mem = 1GB
wal_buffers = 120MB
effective_cache_size = 1GB
default_statistics_target = 100
autovacuum_vacuum_scale_factor = 0.0
autovacuum_vacuum_threshold = 20000
wal_level = replica
min_wal_size = 1GB
max_wal_size = 1GB
wal_log_hints = on
archive_mode = on
archive_command = 'cp %p /postgres/app/postgres/pgsql17/pg_arch/%f'
archive_timeout = 120
max_wal_senders = 10
hot_standby = on
log_destination = 'stderr'
logging_collector = on
log_directory = '/postgres/app/postgres/pgsql17/pg_log/'
log_filename = 'postgresql-%Y-%m-%d.log'
log_file_mode = 0600
log_truncate_on_rotation = off
log_rotation_age = 43200
log_rotation_size = 0
password_encryption = 'scram-sha-256'
5. PostgreSQL 기동(Primary)
# /postgres/app/postgres/pgsql17/bin/pg_ctl -D /postgres/app/postgres/pgsql17/data/ start
6. 이중화 구성용 계정 생성(Primary)
postgres=# create user repl_user replication login encrypted password 'postgres' connection limit -1;
7. pg_hba.conf 설정(Primary)
# vi /postgres/app/postgres/pgsql17/data/pg_hba.conf
..(생략)..
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication porsgres trust
host replication repl_user 10.0.0.61/0 trust
host replication repl_user 10.0.0.117/0 trust
8. PostgreSQL 재기동(Primary)
# /postgres/app/postgres/pgsql17/bin/pg_ctl -D /postgres/app/postgres/pgsql17/data/ reload
🚩 To be continued
이번 글에서는 PostgreSQL 이중화 구성의 주요 단계 중 일부를 살펴보았습니다.
아래 링크를 통해 이어서 Standby DB를 복제하여 동기화까지 다뤘습니다.
PostgreSQL 이중화 구성을 완성하고 싶은 분들은 꼭 확인해보세요!
👉 다음 글 보러가기: 🐘 [PostgreSQL] PostgreSQL 17 고가용성(HA) 이중화 설치 가이드(2)
🐘 [PostgreSQL] PostgreSQL 17 고가용성(HA) 이중화 설치 가이드(2)
🔍 들어가며PostgreSQL 17 버전 기준으로 고가용성(HA) 환경을 구축하는 방법에 대해 정리해보았습니다. 🐘 [PostgreSQL] PostgreSQL 17 고가용성(HA) 이중화 설치 가이드(1) 이 글에 이어서 계속 작성해 보
gwon-s.tistory.com
'🗄️ DB_이야기 > # 🐘 PostgreSQL' 카테고리의 다른 글
| 🐘[PostgreSQL] 수동 장애 전환(Fail-Over)과 복구(Fail-Back) (2) | 2025.07.24 |
|---|---|
| 🐘 [PostgreSQL] PostgreSQL 17 고가용성(HA) 이중화 설치 가이드(2) (4) | 2025.07.17 |
| 🐘 [PostgreSQL] Vacuum (1) | 2025.07.11 |
| 🐘 [PostgreSQL] PostgreSQL의 핵심 개념, MVCC란? (1) | 2025.07.10 |
| 🐘 [PostgreSQL] SQL 처리 과정 (0) | 2025.07.10 |