A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/sql/docs/postgres/iam-logins below:

Log in using IAM database authentication | Cloud SQL for PostgreSQL

Skip to main content Log in using IAM database authentication

Stay organized with collections Save and categorize content based on your preferences.

MySQL   |  PostgreSQL   |  SQL Server

This page describes how users and service accounts can log in to Cloud SQL databases using Cloud SQL IAM database authentication. For more information, see IAM authentication.

Before you begin Log in with automatic IAM database authentication

You can configure a Cloud SQL connector to automatically handle authentication to the Cloud SQL instance on behalf of a user or an application. Connectors include the Cloud SQL Auth Proxy, the Go connector, the Java connector, and the Python connector, all of which support automatic IAM database authentication. When using a Cloud SQL connector with automatic IAM database authentication, the IAM account that you use to start the connector must be the same account that authenticates to the database.

Note: You must allow outgoing (or egress) TCP connections to ports 443 and 3307.

To log in using automatic IAM database authentication:

Cloud SQL Auth Proxy Important: If you use the cloud_sql_proxy binary or the --enable_iam_login flag to start the Cloud SQL Auth Proxy, then you are using v1 of the Cloud SQL Auth Proxy. Migrate to v2, cloud-sql-proxy, and use the --auto-iam-authn flag for IAM database authentication. For more information about migrating to v2, see Migrating from v1 to v2.
  1. Authenticate to Google Cloud.

    User

    Authenticate to Google Cloud using Application Default Credentials (ADC).

    Use the gcloud auth application-default login command. For more information, see Set up Application Default Credentials.

    Service account

    To authenticate to Google Cloud using ADC with a service account, you can use service account impersonation or use a service account key. To use service account impersonation, replace SERVICE_ACCOUNT_EMAIL_ADDRESS, and run the following command:

    gcloud auth application-default login --impersonate-service-account SERVICE_ACCOUNT_EMAIL_ADDRESS

    For more information, see Set up Application Default Credentials.

  2. Start the Cloud SQL Auth Proxy with the --auto-iam-authn flag.

    Replace the following:

    ./cloud-sql-proxy --auto-iam-authn INSTANCE_CONNECTION_NAME

    For more information on how to start the proxy, see Start the Cloud SQL Auth Proxy.

    Warning: If you run the Cloud SQL Auth Proxy as a service, keep in mind that it requests the access tokens on behalf of your applications. For this reason, ensure that only trusted users are able to access the address and port or the Unix socket that the Cloud SQL Auth Proxy is listening on.
  3. When you are ready to connect to the instance by using the Cloud SQL Auth Proxy, log in with the psql client.

    Replace the following:

    Run the following command:

    psql -h HOSTNAME \
     -U USERNAME \
     --port PORT_NUMBER \
     --dbname=DATABASE_NAME
     

    For more information on how to connect to the Cloud SQL Auth Proxy, see Connect with the psql client.

Go Java JDBC Java R2DBC Python To open an interactive example for Cloud SQL Python Connector, click here. Log in with manual IAM database authentication Note: If you plan to use the Cloud SQL Auth Proxy, the Go connector, the Java connector, or the Python connector, then see Log in with automatic IAM database authentication.

A user or an application can authenticate to the database using IAM by manually requesting an access token from Google Cloud and presenting it to the database. Using the gcloud CLI, you can explicitly request an OAuth 2.0 token with the Cloud SQL Admin API scope that is used to log in to the database. When you log in as a database user with manual IAM database authentication, you use your email address as the username and the access token as the password. You can use this method with either a direct connection to the database or with a Cloud SQL connector.

In this procedure, you authenticate to Google Cloud, request an access token, and then connect to the database by passing in the token as the password for the IAM database user. Use these steps to connect without the Cloud SQL Auth Proxy.

For these steps, you must:

  • If you are connecting to an instance with public IP, authorize external access to the instance. For more information, see Authorize your machine's IP address for public IP.
  • If you are connecting to an instance with private IP, run the command within the Virtual Private Cloud (VPC) network.
  • Use the gcloud sql generate-login-token command to generate your authentication token.
  • To log in using manual IAM database authentication:

    gcloud
    1. Authenticate to Google Cloud.

      User

      Authenticate to IAM using gcloud auth login. For more information, see Authorize with a user account.

      Service account

      Authenticate to IAM using gcloud auth activate-service-account. For more information, see Authorize with a service account.

    2. Request the access token, and log in with a client.

      Warning: You can use your OAuth 2.0 token to make authenticated requests on your behalf. Make sure to keep it secure, and be careful where you store it.

      Replace the following:

       PGPASSWORD=`gcloud sql generate-login-token` \
       psql "sslmode=require \
       hostaddr=HOSTNAME \
       user=USERNAME \
       dbname=DATABASE_NAME" \
       --no-password
       

      If ssl_mode on your Cloud SQL instance is configured to TRUSTED_CLIENT_CERTIFICATE_REQUIRED, then we recommend that you log in with automatic IAM database authentication to enforce client identity validation.

      Note: The OAuth 2.0 token can't be entered or pasted directly into a password field, because that token is longer than the field's maximum size. Use an environment variable to pass the authentication token to the command. The provided command is for Unix/Linux environments. If you use Microsoft Windows, substitute Windows environment variable settings.
    What's next

    Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

    Last updated 2025-08-14 UTC.

    [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[],[]]


    RetroSearch is an open source project built by @garambo | Open a GitHub Issue

    Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

    HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4