Question: How do I stop PostgreSQL replication?


Stopping PostgreSQL replication involves different approaches depending on your setup (e.g., streaming replication, logical replication). Below, we provide guidance for both common types.

Streaming Replication

For a Streaming Replication setup, follow these steps:

  1. On the Standby: Stop the PostgreSQL service to ensure it no longer connects to the primary database.

    sudo systemctl stop postgresql
  2. On the Primary: Identify and remove the replication slot (if used) to prevent the primary from retaining WAL files unnecessarily.

    SELECT pg_drop_replication_slot('slot_name');

    Replace 'slot_name' with the name of your replication slot.

  3. Configuration Cleanup: On the primary, you may also want to remove or comment out any replication-related configuration in postgresql.conf and pg_hba.conf, such as max_replication_slots, max_wal_senders, and any specific replication user access rules.

  4. Restart the Primary (optional): If you made changes to the configuration files on the primary server, restart PostgreSQL to apply them.

    sudo systemctl restart postgresql
  5. Replication Slot Check: It's good practice to check if the replication slot has been successfully removed.

    SELECT * FROM pg_replication_slots;

Logical Replication

In the case of Logical Replication, here is what you need to do:

  1. Drop Subscription (on the subscriber database):

    DROP SUBSCRIPTION subscription_name;
  2. Drop Publication (on the publisher database):

    DROP PUBLICATION publication_name;
  3. Configuration Review: Similar to streaming replication, review and adjust any logical replication-specific configurations within postgresql.conf and pg_hba.conf.

Note: Always ensure you have backups before making significant changes to your database infrastructure, especially when stopping replication processes."

Additional Tips

  • After stopping replication, monitor your systems closely. Stopping replication can lead to increased load or other unforeseen issues on your primary database.
  • Consider the impact on disaster recovery and data redundancy strategies once replication is stopped.
  • Documentation for PostgreSQL is an excellent resource for in-depth understanding and troubleshooting.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.