Configure new and existing instances for IAM database authentication

This page has procedures for creating or editing Cloud SQL instances to allow users, service accounts, or groups that are configured to use Cloud SQL IAM database authentication. To learn more about the Cloud SQL IAM integration, see IAM 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 Google Cloud project.

  4. Install the Google Cloud CLI.
  5. To initialize the gcloud CLI, run the following command:

    gcloud init
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. 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.

Configure new instances for IAM database authentication

To configure a new Cloud SQL instance for IAM database authentication, you need to enable the cloudsql.iam_authentication flag. After you enable this flag, you can add IAM users, service accounts, or groups to the Cloud SQL instance.

To configure a new Cloud SQL instance to use 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. Click Choose PostgreSQL.
  4. Enter a name for the Instance ID.
  5. Don't include sensitive or personally identifiable information in your instance name; it's externally visible. You don't need to include the project ID in the instance name. The project ID is included automatically where appropriate (for example, in the log files).
  6. Enter a password for the default administrator.
  7. From the Database version menu, select a database version.
  8. In the Choose region and zonal availability 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.
  9. In the Customize your instance section, click Show Configuration Options, and then expand Flags.
  10. Click Add Flag.
  11. From the Choose a flag menu, select the cloudsql.iam_authentication flag. Make sure that On is selected as the value for this flag, and then click Done.
  12. Configure other instance settings, as needed. For more information about settings, see Settings.
  13. Click Create Instance.

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 PostgreSQL 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

Terraform

To create an instance with IAM database authentication enabled, use a Terraform resource.

resource "google_sql_database_instance" "default" {
  name             = "postgres-db-auth-instance-name-test"
  region           = "us-west4"
  database_version = "POSTGRES_14"
  settings {
    tier = "db-custom-2-7680"
    database_flags {
      name  = "cloudsql.iam_authentication"
      value = "on"
    }
  }
  # 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

Don't include sensitive or personally identifiable information (PII) in your instance name; it is externally visible.

You don't need to include the project ID in the instance name. The project ID is included automatically where needed (for example, in the log files).

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

  • INSTANCE_ID: the desired instance ID
  • REGION: the desired region, such as us-east-1
  • PROJECT_ID: the 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/v1/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/v1/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/v1/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.

REST v1beta4

Don't include sensitive or personally identifiable information (PII) in your instance name; it is externally visible.

You don't need to include the project ID in the instance name. The project ID is included automatically where needed (for example, in the log files).

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

  • INSTANCE_ID: the desired instance ID
  • REGION: the desired region, such as us-east-1
  • PROJECT_ID: the 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.

Configure existing instances for IAM database authentication

To configure an existing Cloud SQL instance for IAM database authentication, you need to enable the cloudsql.iam_authentication flag. After you enable this flag, you can add IAM users, service accounts, or groups to the Cloud SQL instance.

To configure an existing Cloud SQL instance to use IAM database authentication:

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 Customize your instance section, expand Flags.
  5. Click Add Flag.
  6. From the Choose a flag menu, select the cloudsql.iam_authentication flag. Make sure that On is selected as the value for this flag, and then click Done.
  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 CLI, see Install the gcloud CLI. For information about starting Cloud Shell, see Use 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

This resets all other existing database flag settings. For further guidance on setting database flags, see Set a database flag.


REST v1

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

  • PROJECT_ID: the 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/v1/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/v1/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/v1/projects/PROJECT_ID/operations/OPERATION_ID",
  "targetProject": "PROJECT_ID"
}

REST v1beta4

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

  • PROJECT_ID: the 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