--- title: การทำ Postgresql 11 Replication tags: บทความ, postgresql, hot-standby, sysadmin, debian, linux, postgresql 11 --- # การทำ Postgresql 11 Replication ## โจทย์ ต้องการทำข้อมูลบน Postgresql DB ซ้ำไปยัง server อีกตัว โดยให้ตัวหลักทำหน้าที่ update ข้อมูล และตัวสำรองทำหน้าที่ query ได้เพียงอย่างเดียว ## สภาพแวดล้อม - Master Server 192.168.56.100 - Slave Server 192.168.56.101 - Postgresql 11 - Debian 10 ## action ### master server 192.168.56.100 - สร้าง db user สำหรับใช้ในการทำ replication ในการสร้าง user โดยใช้ psql นั้นต้องอยู่ภายใต้ `postgres` user profile เพราะฉนั้นใช้คำสั่ง `sudo su postgres` ```bash $ psql -c "CREATE USER replica REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'YOUR_PASSWORD';" ``` - แก้ไขไฟล์ `/etc/postgresql/11/main/pg_hba.conf` กำหนดให้บัญชี replica สามารถเข้าถึง master server ได้จาก slave server `host replication replica 192.168.56.101/24 md5` - แก้ไขไฟล์ `/etc/postgresql/11/main/postgresql.conf` เพื่อกำหนดให้สามารถเข้าถึงได้จาก network และเปิดการใช้งาน WAL replica ``` listen_addresses = 'localhost,192.168.56.100' wal_level = replica max_wal_senders = 10 wal_keep_segments = 64 ``` - หลังจากแก้ไข config files ต่างๆ ให้ออกจาก `postgres` profile ด้วยคำสั่ง `exit` แล้วทำการ restart postgresql service `sudo systemctl restart postgresql` หรือ `sudo service postgresql restart` ### slave server 192.168.56.101 - ทำการหยุด postgresql service `sudo systemctl stop postgresql` - ดำเนินการแก้ไขไฟล์ `/etc/postgresql/11/main/pg_hba.conf` เปิดให้ master server สามารถเข้าถึงได้ `host replication replica 192.168.56.100/24 md5` - แก้ไขไฟล์ `/etc/postgresql/11/main/postgresql.conf` เพื่อตั้งค่าการเข้าถึงจาก network และการทำ replica ``` listen_addresses = 'localhost,192.168.56.101' wal_level = replica max_wal_senders = 10 wal_keep_segments = 64 hot_standby = on ``` - จากนั้นเตรียมการคัดลอกข้อมูลจาก master server โดยต้องลบข้อมูลที่มีอยู่บน /var/lib/postgresql/11/main ออกก่อน `rm -dr /var/lib/postgresql/11/main` - ทำการคัดลอกข้อมูล `pg_basebackup -h 192.168.56.100 -D /var/lib/postgresql/11/main/ -P -U replica --wal-method=fetch` - สร้างไฟล์ /var/lib/postgresql/11/main/recovery.conf โดยมีการตั้งค่า ดังนี้ ``` standby_mode = 'on' primary_conninfo = 'host=192.168.56.100 port=5432 user=replica password=123' trigger_file = '/tmp/MasterNow' ``` - สำหรับ **Postgresql 12** นั้น ไม่ได้ใช้ recovery.conf แล้ว เปลี่ยนเป็นไฟล์เปล่า /var/lib/pgsql/12/data/standby.signal และตั้งค่าการเชื่อมต่อไว้ใน postgresql.conf `primary_conninfo = 'host=[REPLICA_IP] port=5432 user=replication password=[REPLICATION PASSWORD]'` - ทำการ start postgresql service `systemctl start postgresql` ### ตรวจสอบ ทดสอบ - ลองสร้าง database หรือ table บน Master แล้วตรวจสอบบน slave ว่ามีสิ่งที่สร้างหรือไม่  ## อ้างอิง Part III. Server Administration [Chapter 26. High Availability, Load Balancing, and Replication](https://www.postgresql.org/docs/11/high-availability.html) [How to Setup PostgreSQL 11 Replication](https://linuxhint.com/setup_postgresql_replication/)  > ความคิดเห็นคำแนะนำของท่านต่อบทความนี้ จะถูกนำมาใช้ในการพัฒนาปรับปรุงงานเขียนของผม อย่างไงก็รบกวนแสดงความเห็นคิดด้วยนะครับ > > สนับสนุนการเงินได้ที่ > - บัญชีกสิกรไทย 003–3–29344–5 > - https://paypal.me/chookiatj ← เลือกส่งเงินให้เพื่อนนะครับ > ## โฆษณา  
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up