Question: Does PostgreSQL support clustering?

Answer

PostgreSQL supports several forms of clustering, though it's important to differentiate between them based on the intended functionality: high availability, load balancing, and performance enhancement.

High Availability and Load Balancing

For high availability (HA) and load balancing, PostgreSQL can be used with external tools such as Pgpool-II and Patroni:

  • Pgpool-II acts as a middleware between PostgreSQL servers and clients. It provides connection pooling, load balancing, and automatic failover functionalities.

  • Patroni is a more modern solution which handles automatic failover and management of PostgreSQL configuration. It uses distributed configuration stores like etcd, ZooKeeper, or Consul for managing the state of the cluster.

Native Replication Support

PostgreSQL natively supports streaming replication, where one primary server handles writes and multiple standby servers can handle read queries. This setup enhances read capacity and ensures data redundancy. Standbys can be promoted to primary in case of failure, which is a crucial aspect of high availability setups.

Example of setting up a simple streaming replication:

  1. Configure the primary server to allow connections and log shipping. Set these parameters in postgresql.conf:

    wal_level = replica max_wal_senders = 3 wal_keep_segments = 64

    And add appropriate entries to pg_hba.conf to allow standby to connect.

  2. On the standby server, start with a base backup of the primary using pg_basebackup and set up the following in postgresql.conf:

    hot_standby = on

    Create a recovery file (recovery.conf or use standby.signal in later versions) to point to the primary:

    standby_mode = 'on' primary_conninfo = 'host=primary_host port=5432 user=replicator password=secret'

Third-party Clustering Solutions

Apart from Pgpool-II and Patroni, there are also other third-party solutions like Postgres-XL which is designed for horizontal scalability and supports multi-master write-intensive environments across multiple nodes.

In summary, while PostgreSQL does not include built-in clustering for write-scalability, it supports several configurations and third-party tools that enable clustering for various purposes including high availability, disaster recovery, and read scaling.

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.