One log line, several causes#
The application's own log usually says the least: could not connect to database, a pool timeout, or a stack trace from the driver. The same line follows a missing socket, a missing IAM role, a wrong password, a full connection table and a database restart. Each has a different owner:
- The revision's configuration: the Cloud SQL connection, the VPC egress setting, the execution environment or an environment variable changed with the deploy.
- The identity: the service account the revision runs as, its roles in the project that holds the instance, or the Cloud SQL Admin API in either project.
- The database: its users and passwords, its
max_connections, its maintenance, failover and restarts.
A rollback fixes only the first, and only when the previous revision's configuration still works. The two checks below take a few minutes and tell the three apart.
Check 1: find the stage where the attempt stopped#
A connection from Cloud Run to Cloud SQL passes four stages in order. The error the driver or connector raises, not the application's summary of it, names the last stage the attempt reached. Read the full error from the revision's logs, including the cause chain:
gcloud logging read \
'resource.type="cloud_run_revision"
AND resource.labels.service_name="SERVICE"
AND severity>=ERROR
AND timestamp>="START_TIME"' \
--project PROJECT --order=asc --limit=100 \
--format='value(timestamp, resource.labels.revision_name, labels.instanceId, textPayload, jsonPayload.message)'
| Stage | What fails there | What the error says | Read next |
|---|---|---|---|
| 1. Path | The Unix socket under /cloudsql/ is absent, or a TCP connection to the private IP never completes | The socket file does not exist (No such file or directory), or the connect call times out | The revision's Cloud SQL connections and VPC egress settings |
| 2. Authorization | The Cloud SQL Auth Proxy that Cloud Run runs, or a language connector, asks the Cloud SQL Admin API for the instance's details and a certificate | An HTTP 403 from the Admin API, a disabled API, or an unknown instance name | The revision's service account, its roles, and the API in both projects |
| 3. Login | PostgreSQL received the connection and checks the user | FATAL: password authentication failed for user "...", or a user or database that does not exist | The database user, the password the revision reads, IAM database login |
| 4. Admission and use | PostgreSQL refuses a new session, or ends one that was working | SQLSTATE 53300 (too_many_connections), 57P01 (admin_shutdown), or a connection reset mid-query | Connection counts per revision and the instance's own operations |
The stages come from how the connection is built. Cloud Run's built-in Cloud SQL connection mounts a Unix socket at /cloudsql/INSTANCE_CONNECTION_NAME and reaches the instance through the Cloud SQL Auth Proxy; the service account needs the Cloud SQL Client role, and the Cloud SQL Admin API must be enabled. A private IP connection skips the proxy and goes through Direct VPC egress or a Serverless VPC Access connector instead. Connect from Cloud Run · Cloud SQL roles · PostgreSQL error codes · Client authentication problems
A FATAL message from PostgreSQL settles one question at once: the path and the IAM authorization worked, because the server answered. A timeout settles nothing yet, because stages 1, 2 and 4 can all end in one.
Check 2: does the previous revision connect now?#
The stage says where; the previous revision says whether the deploy matters. Compare what the two revisions carry for the first two stages:
for r in PREVIOUS_REVISION NEW_REVISION; do
gcloud run revisions describe "$r" \
--project PROJECT --region REGION \
--format='yaml(metadata.name, metadata.annotations, spec.serviceAccountName)'
done
The run.googleapis.com/cloudsql-instances annotation lists the revision's Cloud SQL connections, and spec.serviceAccountName names the identity its instances run as. Egress settings and the execution environment appear among the other run.googleapis.com/ annotations. Every new revision copies these settings from the service unless the deploy changed them, so a difference here came from a deploy command, a YAML file or a Terraform template. Revision template annotations · Service identity
Then establish whether a fresh instance of the previous revision connects today. An instance that has served for hours holds connections it opened before the problem began, so it proves little. If the previous revision still serves traffic, read its logs for new instances (varlog/system records each start) and for their first database errors. If it serves none, give it a tag and send it a request that reads the database; a tag routes requests to that revision without moving user traffic. Test the previous revision with a tag
| New instance of the previous revision | Revision settings differ at the failing stage | Reading |
|---|---|---|
| Connects | Yes | The deploy changed the connection, identity or path. Route traffic back, then fix the setting in the deploy that dropped it |
| Connects | No | The new code connects differently: another host variable, pool size, driver or connector version. Read the code change |
| Fails at the same stage | No | Not the deploy. The cause sits in IAM, the network or the database, and a rollback will not help |
| Fails at the same stage | Yes | Two things changed. Fix the shared cause first, then compare again |
A worked fixture: one message, three incidents#
The fixture is illustrative. A fictional service orders-api in europe-west1 connects to the PostgreSQL instance shop-prod:europe-west1:orders-db through Cloud Run's built-in connection. Revision orders-api-00017 has served for two days. At 09:12:30 UTC, orders-api-00018 takes all traffic, and within a minute the application logs could not connect to database on most requests.
Incident A. The full error from orders-api-00018:
| Time (UTC) | Revision | Error |
|---|---|---|
| 09:12:41 | orders-api-00018 | connection to server on socket "/cloudsql/shop-prod:europe-west1:orders-db/.s.PGSQL.5432" failed: No such file or directory |
Stage 1: the socket file is missing. The description of orders-api-00017 carries run.googleapis.com/cloudsql-instances: shop-prod:europe-west1:orders-db; orders-api-00018 carries none, because it was deployed with gcloud run services replace from a YAML file that predates the database. orders-api-00017 scaled out at 09:05 and its new instance connected. The deploy dropped the connection. Route traffic back to orders-api-00017, and add the annotation to the YAML file. The socket under /cloudsql is missing
Incident B. Same deploy, different error:
| Time (UTC) | Revision | Error |
|---|---|---|
| 09:12:44 | orders-api-00018 | The Cloud SQL connection reports an HTTP 403 from the Cloud SQL Admin API for shop-prod:europe-west1:orders-db |
Stage 2. Both revisions list the same instance, but orders-api-00017 runs as orders-runtime@shop-prod.iam.gserviceaccount.com and orders-api-00018 as orders-api@shop-prod.iam.gserviceaccount.com, a new account the deploy introduced without granting it the Cloud SQL Client role. The old revision's new instance at 09:05 connected. Grant the role, or deploy with the previous account, and confirm with Policy Troubleshooter before redeploying. A new service account without Cloud SQL Client
Incident C. Same deploy, a third error:
| Time (UTC) | Revision | Error |
|---|---|---|
| 09:12:52 | orders-api-00018 | FATAL: password authentication failed for user "orders_app" |
| 09:13:10 | orders-api-00017 (tag prev) | FATAL: password authentication failed for user "orders_app" |
Stage 3: the server answered, so the socket and IAM worked. Both revisions read the password from the secret orders-db-password at version latest. The request through the tag prev started a new orders-api-00017 instance at 09:13:08, and it fails the same way. The settings match. Nothing in the deploy caused this; the deploy only started new instances, which read the current password. Cloud SQL's operations list shows an UPDATE_USER operation on orders-db at 08:58, and the Data Access audit log, which this project keeps, names orders_app in its cloudsql.users.update entry: someone changed the user's password, and nobody added the new value to the secret. Instances started before 08:58 keep their sessions until those close, which is why the outage looked like the deploy's. Who changed the database password
All three began with the same application line within a minute of the same deploy. The stage and one fresh instance of the previous revision placed each of them.
Where the checks mislead#
A timeout at stage 1 and a timeout at stage 4 look alike from the application. A connection table that is full, a Cloud SQL instance that is restarting and a private IP route that drops packets all end with a client that waited. Before reading the revision, read the instance's operations for the window, gcloud sql operations list --instance INSTANCE, which records MAINTENANCE, FAILOVER, RESTART, UPDATE and UPDATE_USER with their start and end times. Cloud SQL operation types
A fresh instance of the previous revision answers for the moment you start it. A missing secret version, a revoked role or a database at its connection limit fails it too, but an intermittent network fault can let one connection through. Repeat the start when the first one succeeds and the incident is intermittent.
The connector's error text varies by language and by version, and some applications swallow the cause. When the log holds only the application's summary, the stage table still works from the other end: check whether the database logged anything for the attempt, and whether the revision's settings for each stage match the previous revision's.
The rest of this collection#
Path and configuration:
- The socket under /cloudsql is missing
- Which path does this revision use to reach Cloud SQL?
- Private IP connections from Cloud Run time out
- Cloud SQL latency through a Serverless VPC Access connector
- A first-generation revision and the instance's server CA mode
- Direct TCP to a Cloud SQL public IP from Cloud Run
Identity:
- The revision's service account lacks Cloud SQL Client
- A deploy changed the revision's service account
- The Cloud SQL Admin API is disabled
- IAM database login fails from Cloud Run
- A Cloud SQL instance in another project
From the rollout troubleshooting labs, the general method: Bad release or unhealthy dependency? and A Cloud Run revision that is not ready.
Investigate the alert with its evidence#
Proofline starts an investigation from a Cloud Monitoring alert and reads the alert's Cloud Monitoring metrics and Cloud Logging entries for its window, then cites the windows it read. An investigation reports supported findings and leads; it does not decide whether to roll back. Cloud Monitoring incidents in investigations · Current product availability
Questions#
Why does Cloud Run say it cannot connect to Cloud SQL after a deploy?
The deploy may have changed something the connection needs, such as the Cloud SQL connection on the revision, its service account or its VPC egress setting. It may also only have started new instances, which open fresh connections and meet a problem that older instances, already connected, did not. Compare the two revisions' settings and start a fresh instance of the previous revision to tell them apart.
How do I tell an IAM problem from a database password problem?
An IAM problem stops the attempt before PostgreSQL sees it: the Cloud SQL Auth Proxy or connector gets a refusal from the Cloud SQL Admin API. A password problem comes back from PostgreSQL itself as FATAL: password authentication failed, which means the path and IAM authorization worked.
Will rolling back to the previous revision fix a Cloud SQL connection error?
Only when the previous revision's settings still work. If a fresh instance of the previous revision fails at the same stage, the cause is shared, in IAM, the network or the database, and the rollback only moves traffic to instances that will fail as soon as they reconnect.