Tutorial: Run a Postgres cluster

Last updated 3 min read
Report issue

Overview#

This document provides a step-by-step guide for setting up a highly available (HA) PostgreSQL cluster using GritivaCore. The setup includes a primary node, two replicas, and a connection pooler (pgbouncer), all interconnected via gmesh.

Prerequisites#

To successfully set up the PostgreSQL cluster, ensure you have the following:

  • Three virtual machines (VMs) with agents installed and gmesh meshed.
  • An API key from SET. → API keys in the panel, exported as GRITIVA_API_KEY, for the verification steps below.

Installation#

Step 1: Create Primary Scope#

Create the primary scope on VM-1 from the panel at gritiva.com: INFRA → Scopes → New scope, name pg-primary, VM-1, 4 vCPU / 16 GB, isolation medium, network mode mesh so the replicas can reach it. Wait for the status to go from creating to running.

Step 2: Configure Primary PostgreSQL#

Create a PostgreSQL compose for the primary node with the following parameters:

  • wal_level=replica
  • archive_mode=on

Step 3: Create Replica Scopes#

On VM-2 and VM-3, repeat INFRA → Scopes → New scope to create pg-replica-1 and pg-replica-2, and join all three scopes to the same mesh network under MESH → Networks. Check that the peers came up:

BASH
curl -s -H "X-API-Key: $GRITIVA_API_KEY" \
  https://api.gritiva.com/api/v1/mesh/peers/all

Step 4: Initialize Replicas#

Run the following command on each replica to initialize them:

TEXT
pg_basebackup -h pg-primary.scope-vm-1

Step 5: Add pgbouncer Scope#

On VM-1, create one more scope named pgbouncer the same way, on the same mesh network, and configure it to pool connections to all three nodes.

Step 6: Register the Service#

Register PostgreSQL as a service in the panel: open the pgbouncer scope, go to PROJECTS → Services → Add service, name it postgres and point it at port 6432. The service reports initializing and then active once the agent sees it listening.

Step 7: Share the Service#

Grant another scope access from Sharing → Access shares → New share: pick pgbouncer:postgres from the catalog as the resource and my-app as the grantee. Confirm the grant:

BASH
curl -s -H "X-API-Key: $GRITIVA_API_KEY" \
  https://api.gritiva.com/api/v1/access-shares

Step 8: Configure Backup#

Take scope backups from the panel (Scopes → pg-primary → Backups) or on a schedule under INFRA → Scopes → Backups → Schedules, and keep a daily pg_basebackup running inside the cluster as well:

BASH
# backups that exist for a scope
curl -s -H "X-API-Key: $GRITIVA_API_KEY" \
  https://api.gritiva.com/api/v1/scopes/<scope-id>/backups

# is offsite S3 copying configured?
curl -s -H "X-API-Key: $GRITIVA_API_KEY" \
  https://api.gritiva.com/api/v1/backups/s3/status

Creating one on demand is POST /api/v1/scopes/<scope-id>/backups, which is what the panel button calls. Note that offsite S3 copies are not enabled by default — the status endpoint returns {"enabled": false} until the S3_* environment variables are set on the backend — so until you configure them, every copy lives on the same host as the cluster. Ship your pg_basebackup output somewhere else yourself.

Step 9: Failover#

In case of a failure, promote a replica using:

TEXT
pg_promote()

Update the pgbouncer configuration accordingly.

Step 10: Monitor Replication#

Keep an eye on pg_stat_replication for delay and lag thresholds. Host and scope metrics are graphed in Grafana at monitor.gritiva.com.

Troubleshooting#

If you encounter issues, refer to common troubleshooting tips for PostgreSQL clusters. If a whole scope rather than Postgres looks wrong, check the agent on that VM with sudo /opt/gritiva/gritiva-agent scope-status and journalctl -u gritiva-agent.

Conclusion#

This guide provides a comprehensive approach to setting up a PostgreSQL cluster with high availability. For further insights, refer to the official PostgreSQL documentation.