Use the agentsPostgres

The Postgres agent owns the full lifecycle of a PostgreSQL cluster: initial deploy, schema migrations, failover, PITR restore, connection pooling, autovacuum tuning, and storage growth — on AWS, GCP, and Azure.

What it deploys

The agent selects the right topology based on your environment and cloud:

TopologyBest forClouds
CloudNativePG on EKS / GKE / AKSK8s-native, full operator controlAWS, GCP, Azure
Patroni on EC2 / GCE / VMVM-based HA with etcdAWS, GCP, Azure
AWS RDS Multi-AZManaged, minimal opsAWS
AWS Aurora PostgreSQLManaged + auto-scaling storageAWS
GCP Cloud SQLManaged on GCPGCP
Azure Flexible ServerManaged on Azure with ZRS HAAzure

All topologies include:

  • Encrypted storage at rest and in transit (SSL enforced)
  • Automated nightly backups with configurable retention (default 7 days)
  • Prometheus metrics via postgres_exporter or cloud-native monitoring
  • pgBouncer connection pooler for CNPG and Patroni deployments
  • pg_stat_statements enabled by default

The agent refuses to deploy a single-instance topology for production. It will push back and propose a HA topology instead.

Quickstart

A minimal happy-path for a staging Postgres on GCP Cloud SQL:

  1. Start the agent: “Deploy a managed Postgres for our staging environment on GCP.”
  2. Gate 1 — name: Agent asks for a cluster name; reply staging-pg.
  3. Gate 2 — cloud target: Agent shows your connected GCP projects; pick one from the dropdown.
  4. Gate 3 — topology: Agent proposes Cloud SQL with REGIONAL HA for staging; confirm or ask for CNPG on GKE instead.
  5. Gate 4 — sizing: Agent proposes db-custom-2-8192 (2 vCPU / 8 GB, 200 GB PD-SSD). For staging you can accept or downsize.
  6. Gate 5 — review: Agent renders the Terraform plan (Cloud SQL instance, private VPC peering, backup config, Query Insights). An admin approves if your org has approval gates enabled.
  7. Gate 6 — deploy: terraform apply runs. Typical deploy time ~5 minutes for Cloud SQL, ~12 minutes for CNPG on a fresh GKE cluster.

After deploy the agent outputs:

  • Private IP / connection name (Cloud SQL) or service DNS (CNPG)
  • Secrets Manager path for the app user password
  • Connection string template

Configuration options

OptionDefaultDescription
pg_version16PostgreSQL major version (14, 15, 16)
instance_count3Number of CNPG / Patroni replicas
storage_size_gb200 (staging) / 1024 (prod)Disk per replica; plan 3× projected data
instance_classm5.2xlarge / db-custom-4-16384Compute size — see sizing table
backup_retention7Days of automated backups (1–35)
pgbouncertrueDeploy pgBouncer connection pooler alongside (CNPG/Patroni only)
pool_modetransactionpgBouncer pool mode: session, transaction, statement
max_connections200Hard cap on Postgres backends; remainder served by pgBouncer

Sizing reference

WorkloadAWS RDSGCP Cloud SQLAzure FlexibleCNPG / Patroni
Dev / testdb.t4g.mediumdb-custom-2-4096Burstable B2sm5.large
Small proddb.r6g.largedb-custom-2-8192GP D2s_v3m5.large
Standard proddb.r6g.xlargedb-custom-4-16384GP D4s_v3m5.2xlarge
Heavy proddb.r6g.2xlargedb-custom-8-32768GP D8s_v3m5.4xlarge

Memory tuning formula: shared_buffers = 25% RAM, effective_cache_size = 75% RAM, work_mem = 16 MB (OLTP).

Common patterns

Scaling up

Tell the agent: “Scale our production Postgres to the next tier.”

The agent will:

  1. Identify the current instance class
  2. Propose the next tier (e.g. db.r6g.xlargedb.r6g.2xlarge) and estimate cost delta
  3. For RDS/Cloud SQL/Azure: generate a modify-db-instance or patch call (brief downtime on RDS Multi-AZ failover ~60 s; zero downtime on Aurora / Cloud SQL Regional)
  4. For CNPG: rolling replacement of pods — zero downtime

Planned failover

Tell the agent: “Promote a standby to primary for maintenance.”

The agent runs the correct command per topology:

  • CNPG: kubectl cnpg promote <cluster> <instance> -n <ns>
  • Patroni: patronictl switchover --master <primary> --candidate <standby>
  • RDS: aws rds reboot-db-instance --force-failover
  • Aurora: aws rds failover-db-cluster

Replication lag is verified to be < 1 MB before promoting.

Point-in-time restore (PITR)

Tell the agent: “Restore to 5 minutes before 14:00 UTC today without touching the live cluster.”

The agent creates a new cluster from the backup archive at the target time, validates row counts on the recovered object, and guides you through extracting and importing specific tables back into production.

Adding a read replica

Tell the agent: “Add a read replica for our analytics queries.”

  • CNPG: patches spec.instances up by 1
  • RDS: aws rds create-db-instance-read-replica
  • Aurora: adds a reader instance to the cluster
  • Patroni: extends the ASG desired count

Connecting from another service

Connection string pattern for CNPG:

postgresql://app:<secret>@<cluster>-rw.<namespace>.svc.cluster.local:5432/app?sslmode=require

For RDS / Cloud SQL / Azure, use the managed endpoint with IAM auth or Secrets Manager rotation.

Operational tasks

The agent monitors and handles:

SignalAutomated response
Replication lag > 30sAlerts; investigates ISR / network issues
Disk > 80%Proposes online storage grow (irreversible; no shrink path)
Connection saturation > 80% of max_connectionsProposes pgBouncer pool size increase or max_connections tune
Cache hit ratio < 95%Diagnoses missing indexes or insufficient shared_buffers
Autovacuum stall / XID wraparound warningRuns VACUUM FREEZE VERBOSE ANALYZE on the worst offenders
Long-running queries > 10 minOffers pg_cancel_backend (graceful) then pg_terminate_backend

Key Prometheus metrics exposed:

MetricAlert threshold
pg_stat_replication_lag_seconds> 30 s for 5 min
pg_stat_activity_count / pg_settings_max_connections> 80% for 5 min
node_filesystem_avail_bytesdisk > 80% used
pg_stat_database_blks_hit / blks_readcache hit ratio < 95%

Troubleshooting

SymptomLikely causeFix
FATAL: too many connectionsmax_connections exhaustedAsk agent to increase pgBouncer pool or add replicas
Replication lag climbing steadilyLong-running query on primary blocking WAL applyAgent runs pg_cancel_backend on offending PID
database must be vacuumed within N transactionsAutovacuum fell behind on a hot tableAgent runs VACUUM FREEZE VERBOSE ANALYZE
Disk usage spikeWAL accumulation from an idle replication slotSELECT * FROM pg_replication_slots; drop unused slots
Backup failing on CNPGIRSA role missing S3 write permissionAgent shows the missing IAM policy and patches the role
pg_restore errors after PITRTarget time before the oldest base backupChoose a later target time or widen backup retention
Cloud SQL connection refusedPrivate Service Connect not peeredCheck VPC peering; agent can re-apply the Terraform peering block

See also

Was this page helpful?