Sub topic 3
Preparing the publisher and subscriber: wal_level, roles, and connections
Logical replication needs server settings, a role, and network access before you run CREATE PUBLICATION or CREATE SUBSCRIPTION. Most failed first setups trace back to one of these three. This chapter walks through each one and shows how to check it.
This chapter assumes you can edit postgresql.conf and pg_hba.conf, and that you know what a WAL (write-ahead log) is. It also assumes you know the basics of roles and GRANT. The examples use PostgreSQL 15 or later. The settings also work on earlier versions, except where the text says otherwise.
What each side needs
The publisher is the database that sends changes. The subscriber connects to it and applies them. The two sides need different things.
| Requirement | Publisher | Subscriber |
|---|---|---|
| `wal_level = logical` | Required | Not required unless it also publishes |
| Slot capacity | `max_replication_slots` | `max_replication_slots` (for origin tracking) |
| Sender or worker capacity | `max_wal_senders` | `max_logical_replication_workers` |
| Role | Login role with `REPLICATION` and `SELECT` | Role that creates the subscription and can write to target tables |
| Network | Reachable port, `pg_hba.conf` entry | Outbound access to the publisher |
The subscriber connects to the publisher. The publisher never connects to the subscriber, so the firewall rules only need to allow traffic in that one direction.
Setting wal_level on the publisher
The wal_level setting controls how much information PostgreSQL writes to the WAL. The default is replica. That level is enough for physical replication and archiving, but it does not record what logical decoding needs, such as the old row values for updates and deletes. The logical level adds that information.
-- On the publisher (changing wal_level requires a server restart)
ALTER SYSTEM SET wal_level = 'logical';
-- After the restart, confirm the setting
SHOW wal_level;ALTER SYSTEM writes the value to postgresql.auto.conf. A plain SELECT pg_reload_conf() does not apply it, so restart the server. Until you restart, SHOW wal_level still returns the old value. You can see the pending change in pg_settings:
SELECT name, setting, pending_restart
FROM pg_settings
WHERE name = 'wal_level';A pending_restart value of t means the file has the new value but the running server does not.
Setting wal_level = logical makes the WAL somewhat larger. It adds only a little for most workloads. Workloads that update many rows can notice more, especially on tables with REPLICA IDENTITY FULL (covered in a later chapter).
Capacity settings
Several settings limit how many replication connections and workers the servers can run. The defaults are enough for one or two small subscriptions. They run out quickly when you add more.
| Setting | Where | What it limits | Default |
|---|---|---|---|
| `max_replication_slots` | Publisher | One slot per subscription, plus slots used by table synchronization | 10 |
| `max_wal_senders` | Publisher | Concurrent walsender processes. Each active slot uses one. | 10 |
| `max_replication_slots` | Subscriber | Replication origins, one per subscription plus some for table sync | 10 |
| `max_logical_replication_workers` | Subscriber | Apply workers, one per subscription, plus table-sync workers | 4 |
| `max_sync_workers_per_subscription` | Subscriber | Parallel table copies per subscription | 2 |
| `max_worker_processes` | Subscriber | Total background workers. It must be larger than the logical worker limit. | 8 |
Plan for the initial copy. Each table being copied uses its own temporary slot on the publisher and its own worker on the subscriber. A subscription to 20 tables with max_sync_workers_per_subscription = 2 copies two tables at a time. The rest wait. If the publisher runs out of slots or walsenders during the copy, the sync worker logs an error and retries.
Count slots across everything that uses them. Physical standbys that use slots, other subscriptions, and tools such as Debezium all draw from the same max_replication_slots pool on the publisher.
Changes to max_replication_slots, max_wal_senders, max_worker_processes, and max_logical_replication_workers all need a restart. Make these changes in the same restart as wal_level.
Creating the replication role
The subscriber needs a role on the publisher. The role must meet these conditions:
- It has the
LOGINattribute. - It has the
REPLICATIONattribute, or is a superuser. - It has
SELECTon every published table, because the initial copy reads the tables with ordinary queries.
-- On the publisher
CREATE ROLE repl_user WITH LOGIN REPLICATION PASSWORD 'change_me';
-- Allow the initial copy to read the published table
GRANT SELECT ON orders TO repl_user;Use a dedicated role instead of a superuser. The REPLICATION attribute lets the role open replication connections and create slots. It does not give access to table data on its own. For that reason you still grant SELECT.
Grant SELECT on each table you publish. If you add a table to the publication later, grant SELECT on it too. The grant does not follow automatically. A missing grant shows up as a permission error from the table-sync worker in the subscriber log.
A role also needs privileges to create the publication itself. The user who runs CREATE PUBLICATION needs CREATE on the database. They must also own each table that is published. FOR ALL TABLES requires a superuser.
Configuring pg_hba.conf
The pg_hba.conf file decides who can connect from where. Add a line that matches the subscriber's address, the database, and the role:
# TYPE DATABASE USER ADDRESS METHOD
host shop repl_user 10.0.0.0/24 scram-sha-256A common mistake involves the special replication keyword in the DATABASE column. That keyword matches only physical replication connections, such as those from pg_basebackup and streaming standbys. A logical replication connection is tied to a specific database, so the DATABASE column must name that database (shop here), not replication. A line with only replication accepts standbys but rejects subscribers. The error is no pg_hba.conf entry for replication connection... or similar wording, depending on the version. When it appears, check the database field first.
Reload the configuration after you edit the file. A reload is enough for pg_hba.conf, and no restart is needed:
SELECT pg_reload_conf();Also check listen_addresses in postgresql.conf. The default is localhost, which accepts no remote connections. Set it to the publisher's address or to '*', then restart. If you changed wal_level, you can make this change in the same restart.
Authentication from the subscriber
The subscription stores its connection string in the pg_subscription catalog. That includes the password if you put one in the string. Only superusers can read the stored string. This protection helps, but it does not make the password invisible to administrators.
For passwords, you can keep them out of the catalog by using a .pgpass file or a certificate on the subscriber server. The file belongs to the operating-system user that runs PostgreSQL, and it must have permissions 0600. Then you leave password= out of the connection string.
By default, a non-superuser subscription owner must supply a password in the connection string, and the publisher must actually ask for one. This blocks a subscription owner from using the server's own trust-based access. In PostgreSQL 16 and later, the pg_create_subscription role lets a non-superuser create subscriptions, and the password_required option controls this check. Treat password_required = false as an administrator-only setting.
Preparing the subscriber
The subscriber needs less configuration. It does not need wal_level = logical unless it will also publish changes onward or act as a publisher in its own right. It does need the capacity settings above.
The role that creates the subscription must be a superuser, or in PostgreSQL 16 and later a member of pg_create_subscription with CREATE on the database. The subscription applies changes with the privileges of its owner. In earlier versions the apply worker runs as a superuser, so those privileges do not matter. In recent versions the owner needs INSERT, UPDATE, DELETE, and TRUNCATE on the target tables, as applicable to the operations being published.
Logical replication does not create tables. The target tables must already exist with compatible column names and types. Create them before you create the subscription.
Verifying before you subscribe
Test each layer separately, from the subscriber host, so you find the failing layer quickly:
# 1. Network, pg_hba.conf, and password
psql "host=publisher.example.com port=5432 dbname=shop user=repl_user" -c "SELECT 1;"
# 2. The role can read the published table
psql "host=publisher.example.com port=5432 dbname=shop user=repl_user" -c "SELECT count(*) FROM orders;"Then confirm the publisher settings with one query:
SELECT name, setting
FROM pg_settings
WHERE name IN ('wal_level', 'max_replication_slots', 'max_wal_senders');If the first command fails, the problem is the network, listen_addresses, pg_hba.conf, or the password. If it succeeds and the second fails, the problem is the SELECT grant. If both succeed and CREATE SUBSCRIPTION still fails with a message about wal_level, the restart has not taken effect.
Exercise
A subscriber gets a "no pghba.conf entry" error when it connects to a publisher. The publisher's pghba.conf has the line "host replication repl_user 10.0.0.0/24 scram-sha-256", and a physical standby at 10.0.0.5 connects with it without trouble. What is wrong, and how do you fix it?
Show answer
The keyword "replication" in the DATABASE column matches only physical replication connections. A logical replication connection is made to a specific database, so it needs its own line naming that database, for example "host shop repluser 10.0.0.0/24 scram-sha-256". After editing the file, run SELECT pgreload_conf(); no restart is needed.
Exercise
You changed wallevel to logical with ALTER SYSTEM and ran SELECT pgreloadconf(). SHOW wallevel still returns "replica". Why, and how can you confirm the change is pending?
Show answer
wallevel can only change at server start, so a reload does not apply it. Restart the server. Before the restart, query pgsettings for name = 'wallevel'. The pendingrestart column shows t while the new value is waiting for the restart.