PostgreSQL streaming replication with physical replication slot

Create dedicated replication user:

Text
CREATE ROLE replica WITH REPLICATION PASSWORD 'xxxYYYzzz' LOGIN;

Then add physical replication slot, in order to stay in sync with WAL. Without it, if applying logs on replica will be too slow, main server could drop logs which replica did not consume yet, leading to broken replication:

Text
select pg_create_physical_replication_slot('replica');

Then stop replica server and remove PGDIR/VERSION/main folder. Start copying:

Text
pg_basebackup -D /var/lib/postgresql/14/main -P --wal-method=stream -R --checkpoint=fast -U replica --slot=replica -W -h a.b.c.d

Adjust configuraton discrepancies between main and replica servers, such connections limit and then start replica server.

To verify replication status, run on main server:

SQL
 select * from pg_replication_slots;
View as Markdown

All the content on this website is licensed under Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0). Cite as: Michał Sobczak, “PostgreSQL streaming replication with physical replication slot”, 24 January 2025, https://michalasobczak.pl/posts/postgresql-streaming-replication-with-physical-replication-slot.html