Question: What are the most used PostgreSQL replication tools?

Answer

PostgreSQL offers a variety of replication tools, each serving different needs ranging from high availability to load balancing. Here's an overview of some widely-used PostgreSQL replication tools:

1. Built-in Logical and Physical Replication

PostgreSQL itself comes with built-in support for logical and physical replication.

  • Physical Replication: Involves byte-by-byte copying of data from a primary server to one or more standby servers. This type is suitable for disaster recovery, high availability, and read scaling. The standby servers can be either in hot or warm standby modes, allowing for read-only queries when in hot standby.

    -- On primary, set up wal_level to replica and configure primary_conninfo in postgresql.conf wal_level = replica
  • Logical Replication: Enables the selective replication of data at the table level, allowing for use cases like database upgrades, migrations, or consolidating data from multiple databases.

    -- On primary, create a publication CREATE PUBLICATION my_publication FOR TABLE my_table; -- On subscriber, create a subscription to the publication CREATE SUBSCRIPTION my_subscription CONNECTION 'connection_string' PUBLICATION my_publication;

2. Pglogical

Pglogical is an extension providing logical replication for PostgreSQL, offering more flexibility compared to built-in logical replication. It supports multi-master replication and does not require triggers, reducing overhead.

-- Install pglogical and configure on both primary and subscriber CREATE EXTENSION pglogical;

3. BDR (Bi-Directional Replication)

BDR is an advanced multi-master replication system for PostgreSQL, allowing for very high availability databases. It supports conflict resolution and can replicate across different geographical locations.

-- Requires compiling PostgreSQL with BDR support or using a pre-packaged version that includes BDR. CREATE EXTENSION bdr;

4. Slony-I

Slony-I is an older trigger-based replication system for PostgreSQL. It provides more granularity than physical replication as it operates at the SQL level, but it introduces significant overhead compared to newer logical replication solutions.

# Typically requires installing Slony-I binaries and creating a configuration script slonik <<EOF cluster name = my_cluster; node 1 admin conninfo = 'dbname=mydb host=host1 user=user'; node 2 admin conninfo = 'dbname=mydb host=host2 user=user'; init cluster ( id=1, comment='Primary Node'); create set (id=1, origin=1, comment='Replication Set'); EOF

Each tool has its strengths and specific use cases. The choice among these tools depends on the requirements such as the need for read scaling, zero downtime upgrades, multi-master setups, or geographical distribution.

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.