1. Answers
  2. Setting up GCP SQL Postgres with Failover and Replicas

How do I set up GCP SQL Postgres with failover and replicas?

In this guide, we will set up a PostgreSQL instance on Google Cloud SQL with high availability by configuring failover and replicas. This setup ensures that your database remains accessible and operational in the event of a failure.

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

// Define the primary PostgreSQL instance
const primaryInstance = new gcp.sql.DatabaseInstance("primary-instance", {
    databaseVersion: "POSTGRES_13",
    region: "us-central1",
    settings: {
        tier: "db-f1-micro",
        availabilityType: "REGIONAL",
        backupConfiguration: {
            enabled: true,
            startTime: "03:00",
        },
    },
});

// Define a replica of the primary instance
const replicaInstance = new gcp.sql.DatabaseInstance("replica-instance", {
    databaseVersion: "POSTGRES_13",
    region: "us-central1",
    masterInstanceName: primaryInstance.name,
    instanceType: "READ_REPLICA",
    settings: {
        tier: "db-f1-micro",
    },
});

// Define a failover replica
const failoverReplica = new gcp.sql.DatabaseInstance("failover-replica", {
    databaseVersion: "POSTGRES_13",
    region: "us-central1",
    masterInstanceName: primaryInstance.name,
    instanceType: "FAILOVER_REPLICA",
    settings: {
        tier: "db-f1-micro",
    },
});

// Export the connection name of the primary instance
export const primaryInstanceConnectionName = primaryInstance.connectionName;

// Export the connection name of the replica instance
export const replicaInstanceConnectionName = replicaInstance.connectionName;

// Export the connection name of the failover replica
export const failoverReplicaConnectionName = failoverReplica.connectionName;

Key Points

  • Primary Instance: The main PostgreSQL instance configured with regional availability and daily backups.
  • Replica Instance: A read replica of the primary instance to offload read traffic and improve performance.
  • Failover Replica: A failover instance that takes over in case the primary instance becomes unavailable.

Summary

In this setup, we created a highly available PostgreSQL setup on Google Cloud SQL by defining a primary instance, a read replica, and a failover replica. This configuration ensures that your database remains operational and performant even in the case of failures.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up