물리적 복제(스트리밍)
읽기 전용 대기 서버를 생성하도록 물리적 스트리밍 복제를 설정하고 구성하는 방법을 배웁니다.
물리적 복제(스트리밍)은(는) CoddyKit의 무료 Advanced PostgreSQL: Indexing, Partitioning, Replication 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Advanced PostgreSQL: Indexing, Partitioning, Replication 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Advanced PostgreSQL: Indexing, Partitioning, Replication 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Physical Replication: A Byte-for-Byte Copy
Physical replication in PostgreSQL creates an exact, byte-for-byte copy of your primary database server. Think of it as mirroring the entire data directory and all changes that happen.
This method is highly robust for disaster recovery and provides a consistent read-only copy of your data, known as a standby server. If your primary server fails, you can promote the standby to become the new primary.
How Streaming Replication Works
Streaming replication is a form of physical replication where the primary server continuously sends its Write-Ahead Log (WAL) records to the standby server. The standby then applies these WAL records in real-time.
This ensures the standby is always up-to-date with the primary, minimizing data loss in case of a failure. It's like a constant stream of updates flowing from one database to another.
Primary Config: `wal_level`
To enable streaming replication, the primary server must generate enough WAL information. The wal_level parameter controls this.
It must be set to replica (or logical for more advanced features) in your postgresql.conf. The default minimal level is not sufficient for replication.
Let's check the current wal_level:
SHOW wal_level;Primary Config: `max_wal_senders`
The primary server needs processes to send WAL data to standby servers. These are called WAL sender processes.
The max_wal_senders parameter determines the maximum number of concurrent connections from standby servers. Set this to at least the number of standbys you plan to have, plus some for potential cascading standbys.
Check your current setting:
SHOW max_wal_senders;Creating a Replication User
It's best practice to create a dedicated PostgreSQL user for replication. This user needs the REPLICATION privilege, which allows it to connect and stream WAL data.
This separation enhances security and makes it clear which connections are for replication purposes.
Here's how you might create one (replace with a strong password):
CREATE USER repl_user WITH REPLICATION ENCRYPTED PASSWORD 'your_secret_password';Allowing Standby Connections (`pg_hba.conf`)
The primary server needs to allow connections from the standby server. This is configured in the pg_hba.conf file, which controls client authentication.
You'll need to add an entry for your replication user, specifying the IP address or range of your standby server(s). This tells PostgreSQL to trust connections from those standbys.
# Example pg_hba.conf entry:
host replication repl_user 192.168.1.100/32 md5Base Backup for Standby
Before a standby can start streaming WAL, it needs an initial copy of the primary's data directory. This is called a base backup.
PostgreSQL provides the pg_basebackup utility to create this snapshot. It copies all data files from the primary to the standby, ensuring they start from a consistent point.
This command is typically run from the standby server:
# Example pg_basebackup command (run on standby):
pg_basebackup -h primary_ip -p 5432 -U repl_user -D /var/lib/postgresql/16/main -F p -Xs -P -RStandby Configuration: `standby.signal`
Once the base backup is in place, the standby server needs to know it's a standby. For PostgreSQL 12 and later, this is signaled by the presence of an empty file named standby.signal in the data directory.
This file tells PostgreSQL to start in recovery mode and look for replication configuration.
Standby Configuration: `primary_conninfo`
The standby server also needs to know how to connect to the primary. This is specified using the primary_conninfo parameter in its postgresql.conf file.
It's a connection string similar to what you'd use with psql, including the primary's host, port, replication user, and password.
You can set this using ALTER SYSTEM on the standby:
ALTER SYSTEM SET primary_conninfo = 'host=primary_ip port=5432 user=repl_user password=your_secret_password application_name=my_standby';Replication Config Check
You've learned about the core parameters for setting up streaming replication. Let's test your knowledge!
Recap: Physical Streaming Replication
In this lesson, we explored physical streaming replication. You learned that it creates a byte-for-byte copy of the primary database by continuously sending WAL records.
- We covered primary server configurations like
wal_levelandmax_wal_senders. - You also learned about creating a dedicated replication user and configuring
pg_hba.conf. - Finally, we touched on preparing the standby with a base backup using
pg_basebackupand configuring its connection usingprimary_conninfo.
Next, we'll put these concepts into practice by setting up a standby server!
자주 묻는 질문
“물리적 복제(스트리밍)” 강의는 무료인가요?
네 — “물리적 복제(스트리밍)” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Advanced PostgreSQL: Indexing, Partitioning, Replication 강의 전체를 잠금 해제할 수 있습니다. Advanced PostgreSQL: Indexing, Partitioning, Replication 강의에는 총 4개의 강의가 포함되어 있습니다.
“물리적 복제(스트리밍)”에서 뭘 배우나요?
읽기 전용 대기 서버를 생성하도록 물리적 스트리밍 복제를 설정하고 구성하는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Advanced PostgreSQL: Indexing, Partitioning, Replication을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Advanced PostgreSQL: Indexing, Partitioning, Replication을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Advanced PostgreSQL: Indexing, Partitioning, Replication은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“물리적 복제(스트리밍)” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Advanced PostgreSQL: Indexing, Partitioning, Replication 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Advanced PostgreSQL: Indexing, Partitioning, Replication 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 복제 개념 이해
- 물리적 복제(스트리밍)
- 대기 서버 설정
- 동기식 복제와 비동기식 복제 비교