Configuring instances for IAM database authentication

This page has procedures for creating or editing Cloud SQL instances to allow users or service accounts that are configured to use Cloud SQL IAM database authentication. To learn more about the Cloud SQL IAM integration, see the Overview of Cloud SQL IAM database authentication.

A newly-created instance has a postgres database.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Install and initialize the Cloud SDK.
  5. Make sure you have the Cloud SQL Admin and Compute Viewer roles on your user account.

    Go to the IAM page

    Learn more about roles and permissions.

Configuring new instances for IAM database authentication

Cloud SQL uses a flag to enable and disable IAM user connections on an instance. In this procedure, you enable that flag.

To configure a new instance that uses Cloud SQL IAM database authentication:

Console

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

    Go to Cloud SQL Instances

  2. Click Create instance.
  3. Select PostgreSQL and click Next.
  4. Enter a name for the Instance ID. Do not include sensitive or personally identifiable information in your instance name; it is externally visible. You do not need to include the project ID in the instance name. The project ID is included automatically where appropriate (for example, in the log files). Note: You cannot reuse an instance name for up to a week after you have deleted the instance.
  5. Enter a password for the root user.
  6. In the Location section, select the region and zone for your instance. Place your instance in the same region as the resources that access it. The region you select can't be modified in the future. Usually, you don't need to specify a zone.
  7. Select a database version.
  8. In the Configuration options section, expand Flags.
  9. Select + Add item.
  10. Enter cloudsql.iam_authentication for the flag name. Make sure that On is selected for this flag.
  11. Configure other instance settings, as needed. For more information about settings, see Settings.
  12. Click Create.

gcloud

Run gcloud sql instances create with the -database-flags parameter set to cloudsql.iam_authentication=on.

Replace the following:

  • INSTANCE_NAME: The name of the new instance.
  • POSTGRES_VERSION: The PosgreSQL version (such as POSTGRES_9_6, POSTGRES_10, POSTGRES_11, or POSTGRES_12).
  • NUMBER_OF_CORES: The number of cores in the machine.
  • AMOUNT_OF_MEMORY: The amount of memory in the machine. A size unit should be provided (such as, 3072MiB or 9GiB).
  • ZONE: Preferred Compute Engine zone (such as us-central1-a, or us-central1-b).
  • PASSWORD: Create a password for the root user.
gcloud sql instances create INSTANCE_NAME \
--database-version=POSTGRES_VERSION \
--cpu=NUMBER_OF_CORES \
--memory=AMOUNT_OF_MEMORY \
--zone=ZONE_NAME \
--root-password=PASSWORD \
--database-flags=cloudsql.iam_authentication=on

REST v1beta4

Do not include sensitive or personally identifiable information in your instance name; it is externally visible.

You do not need to include the project ID in the instance name. The project ID is included automatically where appropriate (for example, in the log files).

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

  • instance-id: The desired instance ID
  • region: The desired region, such as us-east-1
  • project-id: Your project ID
  • location-id: The location ID
  • database-version: Enum string of the database version. For example: POSTGRES_12
  • password: The password for the root user
  • machine-type: Enum string of the machine (tier) type, as: db-custom-[CPUS]-[MEMORY_MBS]

HTTP method and URL:

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

Request JSON body:

{
  "name": "instance-id",
  "region": "region",
  "databaseVersion": "database-version",
  "rootPassword": "password",
  "settings": {
    "tier": "machine-type",
    "backupConfiguration": {
      "enabled": true
    },
    "databaseFlags": [
      {
        "name": "cloudsql.iam_authentication",
        "value": "on"
      }
    ]
  }
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#operation",
  "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id",
  "status": "PENDING",
  "user": "user@example.com",
  "insertTime": "2020-01-01T19:13:21.834Z",
  "operationType": "CREATE",
  "name": "operation-id",
  "targetId": "instance-id",
  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id",
  "targetProject": "project-id"
}
To see how the underlying REST API request is constructed for this task, see the APIs Explorer on the instances:insert page.

Configuring existing instances for Cloud SQL IAM database authentication

To configure IAM database authentication on an existing instance:

Console

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

    Go to Cloud SQL Instances

  2. Click the instance name to open its Overview page.
  3. Click Edit.
  4. In the Configuration options section, expand Flags.
  5. Select + Add item.
  6. Enter cloudsql.iam_authentication for the flag name. Make sure that On is selected for this flag.
  7. Configure other instance settings, as needed. For more information about settings, see Settings.
  8. Click Save.

gcloud

For information about installing and getting started with the gcloud command-line tool, see [Installing Cloud SDK](/sdk/downloads). For information about starting Cloud Shell, see the [Cloud Shell documentation](/shell/docs/starting-cloud-shell).

For this procedure, use gcloud sql instances patch.

Replace the following:

  • INSTANCE_NAME: The name of the new instance.
gcloud sql instances patch INSTANCE_NAME \
--database-flags=cloudsql.iam_authentication=on

REST v1beta4

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

  • project-id: Your project ID
  • location-id: The location ID
  • instance-id: The desired instance ID
  • region: The desired region
  • database-version: Enum string of the database version. For example: POSTGRES_12
  • password: The password for the root user
  • machine-type: Enum string of the machine (tier) type, as: db-custom-[CPUS]-[MEMORY_MBS]

HTTP method and URL:

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

Request JSON body:

{
  "name": "instance-id",
  "region": "region",
  "databaseVersion": "database-version",
  "rootPassword": "password",
  "settings": {
    "tier": "machine-type",
    "backupConfiguration": {
      "enabled": true
    }
    "databaseFlags":
    [
      {
        "name": "cloudsql.iam_authentication",
        "value": "on"
      }
    ]
  }
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "kind": "sql#operation",
  "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id",
  "status": "PENDING",
  "user": "user@example.com",
  "insertTime": "2020-01-01T19:13:21.834Z",
  "operationType": "CREATE",
  "name": "operation-id",
  "targetId": "instance-id",
  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id",
  "targetProject": "project-id"
}

What's next