0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · 课时

搭建备用服务器

逐步了解初始化和配置 PostgreSQL 备用服务器的实际步骤,实现持续的数据同步。

搭建备用服务器 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Welcome to Standby Setup

In this lesson, we'll walk through the practical steps to set up a PostgreSQL standby server. This server will continuously synchronize data from your primary server, acting as a read-replica and a crucial part of your disaster recovery strategy.

We'll cover the necessary configurations on both the primary and the standby.

Primary Prep: Replication User

Before initializing our standby, we need a dedicated user on the primary server with replication privileges. This user will be used by the standby to connect and stream WAL (Write-Ahead Log) data.

Run this SQL command on your primary PostgreSQL server:

CREATE USER repluser REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD 'StrongPassword123!';

Primary Prep: Access Control

Next, we need to tell the primary server to accept connections from our standby server for replication. Edit the pg_hba.conf file on your primary server.

Add an entry like this, replacing 192.168.1.100 with the actual IP address of your standby server:

host    replication     repluser        192.168.1.100/32        md5

Primary Prep: Apply Changes

After modifying pg_hba.conf, PostgreSQL needs to reload its configuration. You can do this without a full restart in most cases.

Execute this command on your primary server:

sudo systemctl reload postgresql

Standby: Initial Data Copy

Now, let's move to the standby server. The first step is to get a base copy of the primary's data directory. We use pg_basebackup for this. This command connects to the primary, copies all its data, and creates necessary recovery configuration files.

Run this command on your standby server, replacing placeholders:

pg_basebackup -h primary_ip -U repluser -D /var/lib/postgresql/16/main -P -R

Decoding pg_basebackup Options

Let's break down the pg_basebackup options:

  • -h primary_ip: Hostname or IP of the primary server.
  • -U repluser: The replication user we created.
  • -D /path/to/data: The data directory on the standby.
  • -P: Show progress during the backup.
  • -R: Crucial! Creates a standby.signal file and appends primary_conninfo to postgresql.conf, configuring the standby for recovery.

Standby: Essential Configuration

The -R option handles most of the recovery setup. However, ensure hot_standby is enabled in the standby's postgresql.conf if you want to use it for read queries.

Open postgresql.conf on the standby and ensure this line is present and uncommented:

hot_standby = on

Standby: Connection String

The -R option should have automatically added a primary_conninfo entry to your standby's postgresql.conf. This tells the standby how to connect to the primary.

Verify it looks similar to this, with your actual primary IP and replication user password:

primary_conninfo = 'host=primary_ip port=5432 user=repluser password=StrongPassword123!'

Starting the Standby

With the data copied and configuration in place, you can now start the PostgreSQL service on your standby server. It will automatically begin connecting to the primary and streaming WAL records.

Execute this command on your standby server:

sudo systemctl start postgresql

Verify Replication Status

To confirm that replication is working, connect to your primary server and query the pg_stat_replication view. You should see an entry for your standby server.

A state of 'streaming' indicates success!

SELECT client_addr, state, sync_state FROM pg_stat_replication;

Check Your Setup

You've just set up a PostgreSQL standby! Let's test your understanding of a key step.

Standby Setup Summary

Great job! You've successfully configured a PostgreSQL standby server for streaming replication. This involves:

  • Creating a replication user on the primary.
  • Configuring pg_hba.conf on the primary.
  • Using pg_basebackup -R on the standby for initial data copy and automatic configuration.
  • Ensuring hot_standby = on on the standby.
  • Starting the standby and verifying its status.

Your standby is now ready to receive continuous updates from the primary!

常见问题解答

「搭建备用服务器」课时是免费的吗?

是的 — 「搭建备用服务器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

「搭建备用服务器」这节课中我会学到什么?

逐步了解初始化和配置 PostgreSQL 备用服务器的实际步骤,实现持续的数据同步。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「搭建备用服务器」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?

能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 理解复制概念
  2. 物理复制(流式)
  3. 搭建备用服务器
  4. 同步复制与异步复制
← 返回 Advanced PostgreSQL: Indexing, Partitioning, Replication