Sub topic 3
Preparing the Publisher and Subscriber: `wal_level`, Roles, and Connections
Assume you know basic SQL, can run psql, understand PostgreSQL roles, and have a general idea of how the write-ahead log (WAL) works. This chapter prepares two databases for logical replication. The source database is the publisher. The destination database is the subscriber.
Configure the publisher
Logical replication reads row changes from WAL. Set wal_level to logical on the publisher. This setting requires a server restart. The publisher also needs enough replication slots and WAL sender processes for its subscriptions.
| Setting | Purpose | Change requires restart |
|---|---|---|
| `wal_level = logical` | Records information needed for logical decoding | Yes |
| `max_replication_slots` | Allows logical replication slots | Yes |
| `max_wal_senders` | Allows processes to send WAL changes | Yes |
Set these values in postgresql.conf, or use ALTER SYSTEM as a database administrator:
ALTER SYSTEM SET wal_level = 'logical';
ALTER SYSTEM SET max_replication_slots = 4;
ALTER SYSTEM SET max_wal_senders = 4;Restart PostgreSQL, then verify the active settings:
SHOW wal_level;
SHOW max_replication_slots;
SHOW max_wal_senders;The values must be large enough for the subscriptions you expect to create. A slot is normally created for each subscription. Leave capacity for maintenance or future subscribers.
Create replication roles
Create a login role on the publisher for the subscriber to use. The REPLICATION attribute allows the role to connect for replication. Grant SELECT on every table that the publication will expose.
CREATE ROLE repl_user
WITH LOGIN REPLICATION PASSWORD 'replace_this_password';
GRANT CONNECT ON DATABASE shop TO repl_user;
GRANT USAGE ON SCHEMA public TO repl_user;
GRANT SELECT ON TABLE public.orders TO repl_user;Use a strong password and store it in the subscriber's .pgpass file when possible. Avoid putting a permanent password directly in shell history, source code, or shared documentation.
The role that creates a publication normally owns the published tables or has the required database privileges. Create the publication after the table grants are ready, or grant access to newly published tables before refreshing the subscription.
Allow the connection
PostgreSQL checks incoming connections through pg_hba.conf. Add a rule on the publisher that permits the replication role to connect from the subscriber's address. Use the subscriber's real network address and a narrow network range.
# TYPE DATABASE USER ADDRESS METHOD
host shop repl_user 10.20.30.15/32 scram-sha-256Reload the publisher after editing the file:
SELECT pg_reload_conf();Use TLS for connections that cross an untrusted network. In the subscription connection string, prefer sslmode=verify-full with the correct certificate and hostname. Test ordinary database access before creating the subscription:
psql "host=publisher.example.com port=5432 dbname=shop user=repl_user sslmode=verify-full"The subscriber needs its own database and compatible table definitions. Logical replication does not copy database roles, schemas, or table definitions. A subscriber administrator must create those objects separately.
Understand the connection path
The subscription worker runs on the subscriber. It connects outward to the publisher using the connection string, reads changes through a replication slot, and applies them to local tables.
Exercise
Which publisher setting must be changed before logical replication can decode row changes, and what must happen afterward?
Show answer
Set wal_level to logical, then restart the publisher because this setting does not take effect from a reload alone.