0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DockerでPostgreSQLのストリーミングレプリケーションを構築してみた

0
Posted at

目的

OSS DB取得を目指して、PostgreSQLの学習のために、dockerを使ってストリーミングレプリケーションを動作させます。

手順

前提条件

dockerのインストール
psqlのインストール

postgresのimageをpull

  • OSS DBの対応バージョンに合わせてPostgreSQL 14をpullします。
docker pull postgres:14.15

ネットワークの作成

  • primaryとstanbyのcontainer同士で通信できるようにネットワークを作成します。
docker network create \
  --subnet=172.30.0.0/16 \
  --gateway=172.30.0.1 \
  network_for_streaming_replication

primary_dbを起動

ストリーミングレプリケーションのプライマリーになるdbを起動します。
configファイルを編集しやすいように./primaryディレクトリーをマウントします。

docker run \
  --name postgresql_primary_db \
  --network network_for_streaming_replication \
  -v ./primary:/var/lib/postgresql/data \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=postgres \
  -e POSTGRES_DB=db_src \
  -p 5432:5432 \
  -d postgres:14.15

primary_dbのpostgresql.confの編集

./primary/postgresql.conf の以下の値を変更します。
(デフォルトのpostgresql.confでコメントアウトされているので外します。)

wal_level = replica
max_wal_senders = 10

primary_dbのpg_hba.confを編集

  • この後実施するpg_basebackupができるようにローカルネットワーク(例では192.168.0.0/16。)の接続を許可します。
  • この後起動するstanby_dbがprimary_dbに接続できるように接続を許可します。
host    replication    all    192.168.0.0/16    trust
host    replication    all    172.30.0.0/16    trust
  • 設定が終わったら、primary_dbをコンテナを再起動します。
docker restart postgresql_primary_db

ベースバックアップの取得

pg_basebackupを使用して、stanby_db用で使用するdataを取得します。

PGPASSWORD=postgres pg_basebackup -h localhost -U postgres -D ./stanby -R -p 5432

stanby_dbの起動

  • ストリーミングレプリケーションで使用する、スタンバイのdbを起動します。
docker run --name postgresql_stanby_db  \
  --network network_for_streaming_replication \
  -v ./stanby:/var/lib/postgresql/data    \
  -e POSTGRES_USER=postgres   \
  -e POSTGRES_PASSWORD=postgres   \
  -e POSTGRES_DB=db_src   \
  -p 5433:5433   \
  -d postgres:14.15

stanby_dbのpostgres.confを編集

  • ./stanby/postgresql.conf の以下の値を変更します。
primary_conninfo = 'host=postgresql_primary_db port=5432 user=postgres password=postgres application_name=standby'
port = 5433
  • postgresql.auto.confを削除します。
  • stanby_dbのコンテナを再起動します。
docker restart postgresql_stanby_db

確認

以上で作業は終了です。レプリケーションされるかテストします。

primary_db

  • テーブルをcreateし、データをinsertして、selectできるか確かめます。
PGPASSWORD=postgres psql -U postgres -h localhost -p 5432 -d db_src -c "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE NOT NULL);"
PGPASSWORD=postgres psql -U postgres -h localhost -p 5432 -d db_src -c "INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com'), ('Bob', 'bob@example.com'), ('Charlie', 'charlie@example.com');"
PGPASSWORD=postgres psql -U postgres -h localhost -p 5432 -d db_src -c "SELECT * FROM users;"

stanby_db

primary_dbと同じ値がselectできることを確認します。

PGPASSWORD=postgres psql -U postgres -h localhost -p 5433 -d db_src -c "SELECT * FROM users;"

後処理

  • 作ったリソースを削除したい場合は以下を実行します。
docker stop postgresql_stanby_db
docker rm postgresql_stanby_db
docker stop postgresql_primary_db
docker rm postgresql_primary_db
docker network rm network_for_streaming_replication
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?