Enable and disable high availability

This page describes how to configure an instance for high availability.

You can configure an instance for high availability when you create the instance, or you can enable high availability on an existing instance.

For more information about high availability, see Overview of the high availability configuration.

Create a new instance configured for high availability

When you create an instance and configure it for high availability, Cloud SQL creates it as a regional instance.

To create an instance configured for high availability:

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Select Create instance.
  3. Select the database engine.
  4. In the Choose region and zonal availability section, select Multiple zones (Highly available).
  5. Expand Specify zones.
  6. Select the primary and secondary zones. The following conditions apply when the secondary zone is used during instance creation or edit:
    • The zones default to Any for the primary zone and Any (different from primary) for the secondary zone.
    • If both the primary and secondary zones are specified, they must be different zones.

  7. Click Save.

    You are returned to the instance page for the primary instance while the instance is updated.

gcloud

  1. Create the regional instance:
    gcloud sql instances create REGIONAL_INSTANCE_NAME \
    --availability-type=REGIONAL \
    --database-version=DATABASE_VERSION \
    --cpu=NUMBER_CPUS \
    --memory=MEMORY_SIZE
    
    For a complete list of available parameters, see the gcloud sql instances create reference page.
  2. Configure the root user:
    gcloud sql users set-password root no-host \
    --instance=REGIONAL_INSTANCE_NAME \
    --password=PASSWORD
    

Terraform

To create an instance with high availability, use a Terraform resource.

resource "google_sql_database_instance" "postgres_instance_ha" {
  name             = "postgres-instance-ha"
  region           = "us-central1"
  database_version = "POSTGRES_14"
  settings {
    tier              = "db-custom-2-7680"
    availability_type = "REGIONAL"
    backup_configuration {
      enabled                        = true
      point_in_time_recovery_enabled = true
      start_time                     = "20:55"
    }
  }
  # set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by
  # use of Terraform whereas `deletion_protection_enabled` flag protects this instance at the GCP level.
  deletion_protection = false
}

Apply the changes

To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.

Prepare Cloud Shell

  1. Launch Cloud Shell.
  2. Set the default Google Cloud project where you want to apply your Terraform configurations.

    You only need to run this command once per project, and you can run it in any directory.

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID

    Environment variables are overridden if you set explicit values in the Terraform configuration file.

Prepare the directory

Each Terraform configuration file must have its own directory (also called a root module).

  1. In Cloud Shell, create a directory and a new file within that directory. The filename must have the .tf extension—for example main.tf. In this tutorial, the file is referred to as main.tf.
    mkdir DIRECTORY && cd DIRECTORY && touch main.tf
  2. If you are following a tutorial, you can copy the sample code in each section or step.

    Copy the sample code into the newly created main.tf.

    Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.

  3. Review and modify the sample parameters to apply to your environment.
  4. Save your changes.
  5. Initialize Terraform. You only need to do this once per directory.
    terraform init

    Optionally, to use the latest Google provider version, include the -upgrade option:

    terraform init -upgrade

Apply the changes

  1. Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:
    terraform plan

    Make corrections to the configuration as necessary.

  2. Apply the Terraform configuration by running the following command and entering yes at the prompt:
    terraform apply

    Wait until Terraform displays the "Apply complete!" message.

  3. Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.

Delete the changes

To delete your changes, do the following:

  1. To disable deletion protection, in your Terraform configuration file set the deletion_protection argument to false.
    deletion_protection =  "false"
  2. Apply the updated Terraform configuration by running the following command and entering yes at the prompt:
    terraform apply
  1. Remove resources previously applied with your Terraform configuration by running the following command and entering yes at the prompt:

    terraform destroy

REST v1

For the complete list of parameters for this request, see the instances:insert page.

Create the regional instance.

  1. Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-name: The instance name
    • database-version: The database version enum string
    • region The GCP region
    • machine-type The machine type

    HTTP method and URL:

    POST https://sqladmin.googleapis.com/v1/projects/project-id/instances

    Request JSON body:

    {
      "name": "instance-name",
      "region": "region",
      "databaseVersion": "database-version",
       "settings": {
         "tier": "machine-type",
         "backupConfiguration": {
             "enabled": true,
             "pointInTimeRecoveryEnabled": true
          },
          "availabilityType": "REGIONAL"
       }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  2. When the instance finishes initializing, update the root password on the instance:

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • password: The new root user password

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/users?host=%25&name=postgres'

    Request JSON body:

    {
       "name": "postgres",
       "host": "nohost",
       "password": "password"}
    

    To send your request, expand one of these options:

    You should receive a successful status code (2xx) and an empty response.

REST v1beta4

For the complete list of parameters for this request, see the instances:insert page.

Create the regional instance.

  1. Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-name: The instance name
    • database-version: The database version enum string
    • region The GCP region
    • machine-type The machine type

    HTTP method and URL:

    POST https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances

    Request JSON body:

    {
      "name": "instance-name",
      "region": "region",
      "databaseVersion": "database-version",
       "settings": {
         "tier": "machine-type",
         "backupConfiguration": {
             "enabled": true,
             "pointInTimeRecoveryEnabled": true
          },
          "availabilityType": "REGIONAL"
       }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  2. When the instance finishes initializing, update the root password on the instance:

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • password: The new root user password

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/users?host=%25&name=postgres'

    Request JSON body:

    {
       "name": "postgres",
       "host": "nohost",
       "password": "password"}
    

    To send your request, expand one of these options:

    You should receive a successful status code (2xx) and an empty response.

For more details about creating an instance, see Creating Instances.

Configure an existing instance for high availability

Configuring an existing instance for high availability causes a few minutes of downtime while the instance is reconfigured.

To configure an existing instance for high availability:

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Click Edit.
  4. In the Auto backups and high availability configuration option section, select High availability (regional). It's also recommended that you enable Automate backups.
  5. Click Save.

    You are returned to the instance page for the primary instance while the instance is updated.

gcloud

Update the instance to be regional:

gcloud sql instances patch INSTANCE_NAME \
--availability-type REGIONAL

For reference information, see gcloud sql instances patch.

REST v1

  1. Check the primary instance to see if automatic backups and pointInTimeRecovery are enabled. These are required for high availability instances.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID

    HTTP method and URL:

    GET https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

    {
      "kind": "sql#instance",
      "state": "RUNNABLE",
      "databaseVersion": "POSTGRES_13"
    }
    

    The output contains a section similar to the following:

      "backupConfiguration": {
        "kind": "sql#backupConfiguration",
        "startTime": "12:00",
        "enabled": true,
        "pointInTimeRecoveryEnabled": true
        }
      
  2. If either enabled or pointInTimeRecoveryEnabled are false, use the instances:patch method to enable them both. To enable backups, set enabled to true and startTime to a value which is the start of the backup window. To enable binary logging, set pointInTimeRecoveryEnabled to true.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • start-time The time in the format "HH:MM"

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id

    Request JSON body:

    {
      "settings":
      {
        "backupConfiguration":
        {
          "startTime": "start-time",
          "enabled": true,
          "pointInTimeRecoveryEnabled": true
        }
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  3. Update the instance to be a regional instance:

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-name: The instance name

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-name

    Request JSON body:

    {
      "settings": {"availabilityType": "REGIONAL" }
    }
    

    To send your request, expand one of these options:

    You should receive a successful status code (2xx) and an empty response.

REST v1beta4

  1. Check the primary instance to see if automatic backups and pointInTimeRecovery are enabled. These are required for high availability instances.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID

    HTTP method and URL:

    GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

    {
      "kind": "sql#instance",
      "state": "RUNNABLE",
      "databaseVersion": "POSTGRES_13",
    }
    

    The output contains a section similar to the following:

      "backupConfiguration": {
        "kind": "sql#backupConfiguration",
        "startTime": "12:00",
        "enabled": true,
        "pointInTimeRecoveryEnabled": true
        }
      
  2. If either enabled or pointInTimeRecoveryEnabled are false, use the instances:patch method to enable them both. To enable backups, set enabled to true and startTime to a value which is the start of the backup window. To enable binary logging, set pointInTimeRecoveryEnabled to true.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • start-time The time in the format "HH:MM"

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

    Request JSON body:

    {
      "settings":
      {
        "backupConfiguration":
        {
          "startTime": "start-time",
          "enabled": true,
          "pointInTimeRecoveryEnabled": true
        }
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  3. Update the instance to be a regional instance:

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-name: The instance name

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-name

    Request JSON body:

    {
      "settings": {"availabilityType": "REGIONAL" }
    }
    

    To send your request, expand one of these options:

    You should receive a successful status code (2xx) and an empty response.

Initiate failover

Testing failover is optional, but is recommended so that you can see how your application responds in the event of a failover.

To learn more about failovers, see the Failover overview.

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Click Failover.
  4. In the Manually trigger a failover dialog box, enter the ID of your instance in the text field, and then click Trigger Failover.

gcloud

Initiate the failover:

gcloud sql instances failover PRIMARY_INSTANCE_NAME

REST v1

  1. Describe the primary instance to get the value of the settingsVersion field.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID

    HTTP method and URL:

    GET https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

    {
      "kind": "sql#instance",
      "state": "RUNNABLE",
      "databaseVersion": "POSTGRES_13"
    }
    
  2. Initiate the failover:

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-name: The instance name
    • settings-version: The settingsVersion from instanceInfo

    HTTP method and URL:

    POST https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-name/failover

    Request JSON body:

    {
      "failoverContext":
         {
            "settingsVersion":"settings-version"
         }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

REST v1beta4

  1. Describe the primary instance to get the value of the settingsVersion field.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID

    HTTP method and URL:

    GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

    {
      "kind": "sql#instance",
      "state": "RUNNABLE",
      "databaseVersion": "POSTGRES_13"
    }
    
  2. Initiate the failover:

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-name: The instance name
    • settings-version: The settingsVersion from instanceInfo

    HTTP method and URL:

    POST https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-name/failover

    Request JSON body:

    {
      "failoverContext":
         {
            "settingsVersion":"settings-version"
         }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

The instance fails over and it isn't available to serve data for a few minutes.

Verify an instance has high availability

To verify an instance has high availability:

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. In the Configuration section, it shows Highly available (regional).

gcloud

gcloud sql instances describe INSTANCE_NAME
The output indicates availabilityType is REGIONAL.

REST v1

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-id: The instance ID

HTTP method and URL:

GET https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#instance",
  "state": "RUNNABLE",
  "databaseVersion": "POSTGRES_13"
}

The output indicates availabilityType is REGIONAL.

REST v1beta4

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-id: The instance ID

HTTP method and URL:

GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#instance",
  "state": "RUNNABLE",
  "databaseVersion": "POSTGRES_13",
}

The output indicates availabilityType is REGIONAL.

Disable high availability on an instance

Before you perform this procedure, make sure there are no operations currently running on the instance.

To disable high availability:

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Click Edit.
  4. Open Auto backups.
  5. In the Availabilitysection, click Single zone.
  6. Click Save. This edit requires a restart of the instance.
  7. When you see the message box, click Save and restart.

gcloud

For reference information, see gcloud sql instances patch.
gcloud sql instances patch INSTANCE_NAME \
--availability-type ZONAL

REST v1

The following request uses the instances:patch method to change the availability type on the instance to ZONAL.

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-name: The instance name

HTTP method and URL:

PATCH https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-name

Request JSON body:

{
  "settings": {"availabilityType": "ZONAL" }
}

To send your request, expand one of these options:

You should receive a successful status code (2xx) and an empty response.

REST v1beta4

The following request uses the instances:patch method to change the availability type on the instance to ZONAL.

Before using any of the request data, make the following replacements:

  • project-id: The project ID
  • instance-name: The instance name

HTTP method and URL:

PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-name

Request JSON body:

{
  "settings": {"availabilityType": "ZONAL" }
}

To send your request, expand one of these options:

You should receive a successful status code (2xx) and an empty response.

Troubleshoot

Issue Troubleshooting
You can't find the metrics for a manual failover. Only automatic failovers go into the metrics.
Cloud SQL instance resources (CPU and RAM) are near 100% usage, causing the high availability instance to go down. The instance machine size is too small for the load.

Edit the instance to upgrade to a larger machine size to get more CPUs and memory.

What's next