0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · レッスン

スタンバイサーバーの構築

継続的なデータ同期を行うPostgreSQLスタンバイサーバーの初期化と設定手順を実践します。

「スタンバイサーバーの構築」はCoddyKit上の無料Advanced PostgreSQL: Indexing, Partitioning, Replicationレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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!

よくある質問

「スタンバイサーバーの構築」レッスンは無料ですか?

はい。「スタンバイサーバーの構築」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Advanced PostgreSQL: Indexing, Partitioning, Replicationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Advanced PostgreSQL: Indexing, Partitioning, Replicationコースには全4レッスンが含まれています。

「スタンバイサーバーの構築」で何を学びますか?

継続的なデータ同期を行うPostgreSQLスタンバイサーバーの初期化と設定手順を実践します。 ブラウザで直接実行するハンズオンコードでAdvanced PostgreSQL: Indexing, Partitioning, Replicationを演習し、24時間対応の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に戻る