Where the failure sits#
With IAM database authentication, the database user is a Google identity and the password is a short-lived OAuth 2.0 access token, valid for one hour. IAM authentication The attempt has passed the path and the connector's IAM authorization by the time it fails, so it sits at stage 3 of the four stages, or later when only the grants are missing.
Before checking the login, confirm which identity the revision runs as, because that identity names the database user:
gcloud run revisions describe REVISION --project PROJECT --region REGION \
--format='value(spec.serviceAccountName)'
A deploy that changed it changed the database user as well. A deploy changed the revision's service account
Check 1: the instance accepts IAM logins#
The cloudsql.iam_authentication flag enables logins from IAM accounts. Its default is off, though the console turns it on for instances it creates. Turning the flag off removes access for every user added for IAM database authentication; password users are unaffected. Instance configuration · Supported flags
gcloud sql instances describe INSTANCE --project INSTANCE_PROJECT \
--format='yaml(settings.databaseFlags)'
Flag changes are instance updates, so an UPDATE operation in gcloud sql operations list --instance INSTANCE dates the change. Operation types
Check 2: the database user exists under the right name#
For a service account, the PostgreSQL user name is the account's email without the .gserviceaccount.com suffix: orders-runtime@shop-prod.iam, not the full address. The login must be lowercase, and the truncated name must fit in 63 characters, or the account cannot be added. Log in with IAM · Restrictions
gcloud sql users list --instance INSTANCE --project INSTANCE_PROJECT \
--format='table(name, type)'
gcloud sql users list An IAM service account appears with the type CLOUD_IAM_SERVICE_ACCOUNT; it is created with gcloud sql users create SA_NAME@PROJECT.iam --type=cloud_iam_service_account. Add an IAM user or service account
| What you find | Reading |
|---|---|
| No user for the revision's account | Never added, or the revision now runs as another account |
A user with the full ...gserviceaccount.com name in the code's user setting | The code passes the wrong name. Strip the suffix |
A user of type BUILT_IN with the account's name | Added as a password user; the type decides the authentication method. Manage users |
Check 3: the account holds Cloud SQL Instance User#
Logging in needs cloudsql.instances.login, which Cloud SQL Instance User (roles/cloudsql.instanceUser) contains. Connecting through the Auth Proxy or a connector needs Cloud SQL Client as well. Before you begin · Cloud SQL IAM roles
Cloud SQL documents the error when Instance User is missing for a service account: The caller does not have permission Connection matched pg_hba.conf line 20: "local all +cloudsqliamuser cloudsql-iam-user". Roles with serverless options
gcloud policy-intelligence troubleshoot-policy iam \
//cloudresourcemanager.googleapis.com/projects/INSTANCE_PROJECT \
--principal-email=SERVICE_ACCOUNT \
--permission=cloudsql.instances.login
Check 4: how the token reaches the database#
There are two ways, and they fail differently. Automatic versus manual
- Automatic. The Auth Proxy, or the Go, Java or Python connector, requests and refreshes the token and passes it as the password; the code passes only the user name. The account that starts the connector must be the account that logs in. In Python,
enable_iam_auth=True; in Go,cloudsqlconn.WithIAMAuthN(). Log in with automatic IAM authentication - Manual. The code fetches a token and passes it as the password. The token expires after one hour, and new connections or logins after that fail. A pool that opens connections hours later with a token fetched at startup fails on its first new connection. IAM logins require SSL, so a manual login over an unencrypted direct connection is rejected. Manual IAM authentication · Restrictions
| Pattern | Reading |
|---|---|
| Every login fails from the first one | Checks 1 to 3, or a connector that is not set to automatic IAM authentication, so no token is sent |
| Logins work for about an hour after an instance starts, then new connections fail | A manual token that is never refreshed |
| Logins slow or time out on a shared-core instance while password logins are fine | IAM logins cost more CPU; Cloud SQL names shared-core machine types as a cause. Cloud SQL troubleshooting |
Check 5: the login works and the first query fails#
A new IAM database user has no privileges on any database except what is granted to PUBLIC. Grant database privileges The error is a permission error from PostgreSQL, SQLSTATE 42501 (insufficient_privilege), not a login failure. PostgreSQL error codes A service account replaced by a new one, or a user deleted and re-added, comes back with no grants.
A worked fixture: the hour and the name#
The fixture is illustrative. The fictional service orders-api in europe-west1 reaches shop-prod:europe-west1:orders-db and logs in as orders-runtime@shop-prod.iam.
Outcome A: a manual token. orders-api-00018, deployed at 09:12:30 UTC, replaced the Python connector with a direct private IP connection and fetches a token once at startup. Its instances serve normally until about 10:13; from then, new connections fail at login while connections already open keep working. Instances started at 09:40 fail from about 10:40. The failures follow each instance's start time plus one hour, not the clock. The release caused it; go back to the connector with enable_iam_auth=True, or refresh the token before each new connection.
Outcome B: a new account. orders-api-00018 runs as orders-api@shop-prod.iam.gserviceaccount.com, and its code sets the user from an environment variable that still says orders-runtime@shop-prod.iam. The connector authenticates as orders-api, which is not the account named in the login, and gcloud sql users list has no orders-api@shop-prod.iam user. The deploy changed the identity. Route traffic back, then add the new account as a database user, grant it Instance User and the old user's table privileges, and set the variable.
Where the checks mislead#
The Cloud Run page for the built-in Cloud SQL connection does not describe automatic IAM login through its embedded proxy. The automatic method is documented for the standalone Auth Proxy with --auto-iam-authn and for the language connectors. Log in with automatic IAM authentication
Cloud SQL limits authorization of logins to 12,000 per minute per instance, counting failures. A crash loop retrying logins across many instances can exhaust it and make healthy instances fail as well. Restrictions
An IAM group user changes the picture: the account logs in with its own identity, and its privileges come from the group. A missing user in check 2 is expected there.
Questions#
What is the Cloud SQL PostgreSQL user name for a service account?
The service account's email without .gserviceaccount.com, such as sa-name@project-id.iam, in lowercase and within 63 characters.
Why do IAM database logins from Cloud Run fail after an hour?
The code passes a manually fetched OAuth 2.0 access token, which is valid for one hour. Connections opened after that fail. A Cloud SQL connector with automatic IAM authentication refreshes the token.
Which roles does a Cloud Run service account need for IAM database login?
Cloud SQL Instance User for cloudsql.instances.login, and Cloud SQL Client when it connects through the Auth Proxy or a language connector, both in the instance's project. It also needs a database user and grants on the tables it reads.