Loading…
Loading documentation...
Run a resilient database cluster with backups and failover.
Self-managed setup. A managed database product is planned and not yet available. This guide sets up PostgreSQL yourself across Linux servers you check out.
Run a PostgreSQL primary with streaming replicas, connection pooling and nightly backups.
| Server | Role | Size |
|---|---|---|
| db-primary | Primary (read/write) | 4 vCPU / 8 GB or more |
| db-replica-1 | Replica (read-only) | the same as the primary |
| db-replica-2 | Replica (read-only) | 2 vCPU / 4 GB or more |
All three servers share your environment's private network, and its firewall lets them reach each other on every port. None of them needs a port forward: the application reaches them over the private network.
Create a Virtual Datacenter first. Enroll a machine in your cloud account or at your site, or choose a currently qualified external region in the VDC. Check the displayed provider, configuration and price before placing an order.
Create the three servers in the same environment. Each joins its private network at creation; its private address is on Servers > [your server] > Network, and the network's range (like 10.0.105.0/24) is on Network > Map. See Private Networking.
On each server, turn on public SSH, sign in with the command from its How to connect card, and run sudo -i for a root shell.
On all three servers:
apt update
apt install -y postgresql-16
Ubuntu 24.04 creates the 16/main cluster and starts it.
On all three servers, in place of PRIVATE_NETWORK write your private network's range:
cat > /etc/postgresql/16/main/conf.d/cluster.conf <<'EOF'
listen_addresses = '*'
wal_level = replica
max_wal_senders = 5
wal_keep_size = '1GB'
hot_standby = on
EOF
cat >> /etc/postgresql/16/main/pg_hba.conf <<'EOF'
host replication replicator PRIVATE_NETWORK scram-sha-256
host all all PRIVATE_NETWORK scram-sha-256
EOF
systemctl restart postgresql
On db-primary only, with a strong password of your own:
sudo -u postgres psql -c "CREATE ROLE replicator WITH REPLICATION LOGIN PASSWORD 'STRONG_REPLICATION_PASSWORD';"
On each replica, replace its data with a copy of the primary's. pg_basebackup asks for the replicator's password:
systemctl stop postgresql
rm -rf /var/lib/postgresql/16/main
sudo -u postgres pg_basebackup -h PRIMARY_PRIVATE_IP -U replicator \
-D /var/lib/postgresql/16/main -Fp -Xs -P -R
systemctl start postgresql
The -R flag writes standby.signal and the connection to the primary, so the copy starts as a replica.
On the primary, both replicas are listed with state = streaming:
sudo -u postgres psql -c "SELECT client_addr, state, sync_state FROM pg_stat_replication;"
On a replica, recovery is on:
sudo -u postgres psql -c "SELECT pg_is_in_recovery();"
On the primary, create your application's user and database, and print the user's password secret for PgBouncer:
sudo -u postgres psql -c "CREATE ROLE appuser LOGIN PASSWORD 'STRONG_APP_PASSWORD';"
sudo -u postgres psql -c "CREATE DATABASE mydb OWNER appuser;"
sudo -u postgres psql -Atc "SELECT rolpassword FROM pg_authid WHERE rolname = 'appuser';"
On your application server, in place of APPUSER_SCRAM_SECRET paste the line the last command printed (it starts with SCRAM-SHA-256):
apt update
apt install -y pgbouncer postgresql-client
cat > /etc/pgbouncer/pgbouncer.ini <<'EOF'
[databases]
mydb = host=PRIMARY_PRIVATE_IP port=5432 dbname=mydb
mydb_ro = host=REPLICA1_PRIVATE_IP port=5432 dbname=mydb
[pgbouncer]
listen_addr = 127.0.0.1
listen_port = 6432
auth_type = scram-sha-256
auth_file = /etc/pgbouncer/userlist.txt
pool_mode = transaction
max_client_conn = 200
default_pool_size = 25
EOF
echo '"appuser" "APPUSER_SCRAM_SECRET"' > /etc/pgbouncer/userlist.txt
chown postgres:postgres /etc/pgbouncer/userlist.txt
chmod 640 /etc/pgbouncer/userlist.txt
systemctl restart pgbouncer
# Through PgBouncer to the primary; it asks for appuser's password
psql "host=127.0.0.1 port=6432 dbname=mydb user=appuser" -c "SELECT 1;"
Your application connects to 127.0.0.1:6432: database mydb for writes, mydb_ro for reads from a replica.
On the primary, a nightly dump runs as the postgres user and keeps seven days:
install -d -o postgres -g postgres -m 700 /var/backups/postgresql
cat > /usr/local/bin/backup-db.sh <<'EOF'
#!/bin/bash
set -euo pipefail
FILE="/var/backups/postgresql/db-backup-$(date +%Y%m%d-%H%M%S).sql.gz"
pg_dumpall | gzip > "$FILE"
find /var/backups/postgresql -name 'db-backup-*.sql.gz' -mtime +7 -delete
echo "Backup complete: $FILE"
EOF
chmod 755 /usr/local/bin/backup-db.sh
echo '0 2 * * * postgres /usr/local/bin/backup-db.sh >> /var/backups/postgresql/backup.log 2>&1' > /etc/cron.d/db-backup
# Run it once now
sudo -u postgres /usr/local/bin/backup-db.sh
A dump on the same server does not survive the server. Copy the dumps to storage you control, such as an S3-compatible bucket.
If the primary fails, promote a replica. On the replica to promote:
pg_ctlcluster 16 main promote
sudo -u postgres psql -Atc "SELECT pg_is_in_recovery();" # f: it takes writes now
Then point your application (or PgBouncer's mydb line) at the new primary, and rebuild the other servers as its replicas with Step 5.
Open the workspace to check your machine’s connection and available controls. For automation, consult the REST API or MCP guide. Include the resource and region when you contact support.