A database can remain writable on two servers only when its changes can travel in both directions without being replayed forever. PostgreSQL active-active with PCS combines bidirectional logical replication with Pacemaker supervision of a separate database instance on each node. PostgreSQL 18 on Ubuntu uses publications and origin-filtered subscriptions for the data exchange.
Each row must have one writing node. Native logical replication does not resolve competing updates, synchronize sequences, or copy schema changes. This arrangement suits applications that partition writes by node or tenant; it does not make arbitrary writes through a round-robin load balancer safe. Replication is asynchronous, so a surviving node can be missing recently committed changes.
Start with two quorate, fenced cluster nodes, PostgreSQL 18 and the resource-agents-extra package installed, and separate local data directories. Both database servers need working TLS certificates matching their node names, with the issuing CA trusted by the postgres account. The setup creates a new, initially empty database; an existing application's data needs a separate synchronization and cutover plan.
Steps to set up PostgreSQL active-active with PCS:
- Confirm that both cluster nodes are online with quorum and working fencing resources.
$ sudo pcs status
The node names below are node-01 and node-02, with private addresses 192.0.2.11 and 192.0.2.12. Both names must resolve between nodes. Fencing must already have passed a controlled test; a resource listing alone does not establish that power control works.
- Back up the PostgreSQL configuration directory on both nodes.
Database restarts and the later handover to Pacemaker interrupt connections. A clone must never open one shared data directory from two nodes. Neither local PostgreSQL instance may already be managed by another cluster resource.
$ sudo cp -a /etc/postgresql/18/main /root/postgresql-18-main.before-active-active
The saved directory preserves the previous listener, authentication, and automatic-start settings. No existing application data is changed by the new aa database.
- Create a file named active-active.conf in /etc/postgresql/18/main/conf.d/ on both nodes using your text editor.
$ sudoedit /etc/postgresql/18/main/conf.d/active-active.conf
listen_addresses = 'node-01' wal_level = logical
On node-02, the listener value is 'node-02'. The packaged defaults provide enough replication slots and workers for this two-node, one-table setup.
- Add peer-only replication access before the first existing host record in /etc/postgresql/18/main/pg_hba.conf on both nodes.
$ sudoedit /etc/postgresql/18/main/pg_hba.conf
hostssl aa repl 192.0.2.12/32 scram-sha-256 host aa repl 0.0.0.0/0 reject host aa repl ::/0 reject
On node-02, the allowed peer address is 192.0.2.11/32. Existing records remain below these entries. Logical replication connects to the named aa database, rather than the special physical-replication database. Network firewalls also need to permit TCP 5432 only between the required peers and approved clients.
- Restart the local PostgreSQL instance on both nodes to apply the listener and logical WAL settings.
$ sudo systemctl restart postgresql@18-main
- Create the empty aa database on both nodes.
$ sudo -u postgres createdb aa
- Create the same replicated table in the aa database on both nodes.
$ sudo -u postgres psql -X -v ON_ERROR_STOP=1 -d aa aa=# CREATE TABLE public.items (node_name text NOT NULL, item_id bigint NOT NULL, note text NOT NULL, PRIMARY KEY (node_name, item_id)); aa=# \q
The application must write only rows belonging to its local node_name, including updates and deletes. The compound key separates locally allocated item numbers; it does not enforce write ownership. Shared unique constraints and cross-node updates need an application-level coordination design. Administrative SQL uses the local postgres account; applications need their own restricted roles.
- Create a replication login with read access to the published table on both nodes.
$ sudo -u postgres psql -X -v ON_ERROR_STOP=1 -d aa aa=# CREATE ROLE repl WITH LOGIN REPLICATION; aa=# GRANT CONNECT ON DATABASE aa TO repl; aa=# GRANT USAGE ON SCHEMA public TO repl; aa=# GRANT SELECT ON public.items TO repl; aa=# \q
The REPLICATION attribute is a privileged capability. This login receives no table-write or superuser privilege.
- Set the repl password interactively on each node.
$ sudo -u postgres psql -X -v ON_ERROR_STOP=1 -d aa aa=# \password repl aa=# \q
Password input is hidden. The password belonging to each publisher is needed by its peer.
- Create a private password file on each node only if /var/lib/postgresql/.pgpass is absent.
The install command empties an existing file. If the file already exists, skip this command and preserve its other entries in the next edit. The file must be owned by postgres with mode 600.
$ sudo install -o postgres -g postgres -m 600 /dev/null /var/lib/postgresql/.pgpass
- Add the peer credentials at the end of /var/lib/postgresql/.pgpass on both nodes using your text editor.
$ sudoedit -u postgres /var/lib/postgresql/.pgpass
node-01:5432:aa:repl:PASSWORD_ON_NODE_01 node-02:5432:aa:repl:PASSWORD_ON_NODE_02
The uppercase values stand for the passwords set on those servers. A literal colon or backslash in a password needs a preceding backslash. The postgres account's trusted CA file is /var/lib/postgresql/.postgresql/root.crt; verify-full checks both trust and hostname.
- Publish the table's row changes on both nodes.
$ sudo -u postgres psql -X -v ON_ERROR_STOP=1 -d aa aa=# CREATE PUBLICATION aa_pub FOR TABLE public.items WITH (publish = 'insert, update, delete'); aa=# \q
Schema changes and sequence state are not replicated. TRUNCATE is deliberately excluded from this publication.
- Subscribe each node to changes from its peer.
$ sudo -u postgres psql -X -v ON_ERROR_STOP=1 -d aa aa=# CREATE SUBSCRIPTION from_node_02 CONNECTION 'host=node-02 dbname=aa user=repl sslmode=verify-full passfile=/var/lib/postgresql/.pgpass' PUBLICATION aa_pub WITH (origin = none, copy_data = false); aa=# \q
The SQL shown is for node-01. On node-02, use subscription from_node_01 with host=node-01.
copy_data = false is suitable here because both tables are empty and application writes have not started. It does not reconcile existing rows. origin = none prevents changes received from the peer being sent back around the loop.
- Replace auto with manual in /etc/postgresql/18/main/start.conf on both nodes.
$ sudoedit /etc/postgresql/18/main/start.conf
manual
- Reload systemd on both nodes to apply the manual startup policy.
$ sudo systemctl daemon-reload
The Ubuntu PostgreSQL service will no longer start this instance automatically.
- Stop the local PostgreSQL instance on both nodes for the handover to Pacemaker.
$ sudo systemctl stop postgresql@18-main
If resource creation fails, the databases remain stopped. Before a manual recovery, any created resource must be disabled and confirmed stopped. The saved configuration and systemctl start postgresql@18-main provide the path back to local administration. While Pacemaker owns the instance, database starts, stops, and restarts belong in PCS.
- Create a cloned PostgreSQL resource from either cluster node.
$ sudo pcs --future resource create postgresql_service ocf:heartbeat:pgsql \ pgctl=/usr/lib/postgresql/18/bin/pg_ctl \ psql=/usr/lib/postgresql/18/bin/psql \ pgdata=/var/lib/postgresql/18/main \ config=/etc/postgresql/18/main/postgresql.conf \ rep_mode=none socketdir=/var/run/postgresql \ op start timeout=60s op stop timeout=60s \ op monitor interval=10s timeout=30s \ clone meta clone-max=2 clone-node-max=1 interleave=trueocf:heartbeat:pgsql is supplied by Ubuntu's resource-agents-extra package. rep_mode=none leaves replication to the logical subscriptions; it does not disable them or create a physical primary/standby pair. Each clone uses the same pathname on its own local disk.
- Confirm that the clone is started on both nodes.
$ sudo pcs status resources * Clone Set: postgresql_service-clone [postgresql_service]: * Started: [ node-01 node-02 ]If either instance is still starting, repeat this check until Started: [ node-01 node-02 ] appears. A Starting, Stopped, or failed instance is not ready for application traffic.
- Insert one locally owned row on each node.
$ sudo -u postgres psql -X -v ON_ERROR_STOP=1 -d aa aa=# INSERT INTO public.items VALUES ('node-01', 1, 'written on node-01'); aa=# \qOn node-02, the statement is INSERT INTO public.items VALUES ('node-02', 1, 'written on node-02');.
- Read both rows from each local database.
$ sudo -u postgres psql -X -d aa -c "TABLE public.items ORDER BY node_name, item_id;" node_name | item_id | note -----------+---------+-------------------- node-01 | 1 | written on node-01 node-02 | 1 | written on node-02 (2 rows)
Both nodes must show both rows after replication catches up. Missing rows or duplicate-key errors require investigation in the PostgreSQL logs before routing writes. An offline peer also retains WAL through its replication slot, so disk use needs monitoring. Client routing must preserve row ownership and exclude unavailable nodes.