PostgreSQL のデータベースのロジカルレプリケーションを設定する

FreeBSD に PowerDNS をインストールする で権威DNSサーバーの primary の設定をしたわけですが、PowerDNS の設定としては Native で、secondary への反映はデータベースのレプリケーションで行う必要があります。その設定を行っていきます。

primary 側の設定

/var/db/postgres/data14/postgresql.conf でレプリケーションの設定をロジカルに変更するとともに、ほかのホストから接続できるようにします。

listen_address = '*'
wal_level = logical

/var/db/postgres/data14/pg_hba.conf に secondary 側から SSL で接続できるように設定します。

hostssl    pdns    all    2001:0db8:0:0:3456:3456:3456:3456/128    trust

postgresql server の再起動が必要です。

# service postgresql restart

postgres role でデータベースに接続して、pdns role に REPLICATION 権限を設定します。

$ psql -U postgres -d pdns
pdns=# ALTER ROLE pdns REPLICATION;

次に、pdns role で pdns データベースのテーブルに対して PUBLICATION を設定します。 下の2つは PUBLICATION が設定されていることの確認です。

$ psql -U pdns -d pdns
pdns=> CREATE PUBLICATION pdns FOR TABLE comments, cryptokeys, domainmetadata, domains, records, supermasters, tsigkeys;
pdns=> SELECT * FROM pg_publication;
pdns=> SELECT * FROM pg_publication_tables;

secondary 側の設定

先程のページの「PostgreSQLの設定」の項にある、 roleの作成、データベースの作成、スキーマの適用までは同じことを順に行います。

次からは primary と異なるところですが、pdns データベースに対して postgres role で SUBSCRIPTION を設定します。

$ psql -U postgres -p pdns
pdns=# CREATE SUBSCRIPTION pdns CONNECTION 'host=2001:0db8:0:0:1234:1234:1234:1234 port=5432 dbname=pdns sslmode=require' PUBLICATION pdns;

補足

SSL 関連の設定については述べませんが、このあたりを参照してください。

動作確認

FreeBSD に PowerDNSAdmin をインストールする で設定した PowerDNS Admin で primary 側でレコードを設定し、secondary サーバーで引けるかどうか確認します。

$ drill host.example.jp @2001:0db8:0:0:3456:3456:3456:3456