What the error tells you#
This is a failure at the first of the four stages: the client never reached a listener. PostgreSQL's client library reports it with the path it tried, for example connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory. A connection failure message always shows the server address or socket path, so the error itself tells you where the client looked. Server start problems
Cloud Run's built-in Cloud SQL connection mounts a Unix socket for each connected instance at /cloudsql/INSTANCE_CONNECTION_NAME, where the connection name has the form project:region:instance-id. PostgreSQL needs a .s.PGSQL.5432 suffix on that path; some libraries add it, others need it written out. MySQL samples pass the /cloudsql/INSTANCE_CONNECTION_NAME path itself as the socket. Linux limits a socket path to 108 characters, and a longer path cannot be used from Cloud Run. Connect with Unix sockets (PostgreSQL) · Connect with Unix sockets (MySQL)
Read the full path from the error, then split it in three: /cloudsql/, the connection name, and whatever the driver appended. Each part has its own check.
Check 1: does the revision carry the connection?#
The Cloud SQL connections of a revision are stored in the run.googleapis.com/cloudsql-instances annotation, a comma-separated list. The Cloud Run v2 API shows the same setting as a volume of type cloudSqlInstance, with each instance available under /cloudsql/[instance]. Revision template annotations · Cloud Run v2 Volume
for r in PREVIOUS_REVISION NEW_REVISION; do
gcloud run revisions describe "$r" \
--project PROJECT --region REGION \
--format='yaml(metadata.name, metadata.annotations, spec.containers[0].env)'
done
Setting a Cloud SQL connection creates a new revision, and later revisions keep it unless a deploy changes it explicitly. Connect from Cloud Run Three kinds of deploy change it:
gcloud run deployorgcloud run services updatewith--set-cloudsql-instances, which replaces the list,--remove-cloudsql-instances, or--clear-cloudsql-instances, which empties it.gcloud run deploygcloud run services replace FILE, which creates or replaces the service from a YAML specification. A file exported before the connection was added carries no annotation, so the new revision has none.gcloud run services replace- Terraform or another tool that writes the whole template. In
google_cloud_run_v2_service, the connection is acloud_sql_instancevolume plus avolume_mountsentry at/cloudsql; a template without them deploys a revision without the socket. Terraform example
| Previous revision | New revision | Reading |
|---|---|---|
| Lists the instance | Lists nothing, or another instance | The deploy dropped the connection. Route traffic back, then restore it in the file or flag that removed it |
| Lists the instance | Lists the same instance | The connection is in place. Go to check 2 |
| Lists nothing | Lists nothing | Neither revision uses the built-in socket; the old code reaches the database another way. Which path the revision uses |
Check 2: does the code name the same instance?#
The directory under /cloudsql/ carries the exact connection name. The code builds its path from its own setting, often an environment variable such as INSTANCE_UNIX_SOCKET or INSTANCE_CONNECTION_NAME in Google's samples. Compare three strings: the annotation, the path in the error, and the instance's own name.
gcloud sql instances describe INSTANCE \
--project INSTANCE_PROJECT --format='value(connectionName)'
connectionName is the instance's project:region:instance name. Instance resource gcloud run deploy accepts a bare instance name when the instance is in the same project and region as the service; anything else needs the full project:region:instance form. gcloud run deploy
| The path in the error | Reading |
|---|---|
| Names a different project, region or instance than the annotation | The code's setting is wrong. Find where the variable changed: the revision's env, a secret, or a default in the code |
Matches the annotation, which differs from connectionName | The connection itself names the wrong instance. Fix the annotation |
| Matches both exactly | The directory is right. Go to check 3 |
Check 3: does the driver look for the right file?#
Most PostgreSQL drivers take the socket directory and append .s.PGSQL.5432 themselves. libpq treats a host value that begins with a slash as the directory holding the socket file. Others expect the full file path, and Google's pg8000 sample passes unix_sock with the suffix written out. A driver change can move the suffix from one side to the other. libpq host · Connect with Unix sockets
| The path the client tried | Reading |
|---|---|
Ends in /cloudsql/CONNECTION_NAME/.s.PGSQL.5432 | Correct for PostgreSQL. If it is missing, check 1 or check 2 explains why |
Ends in /cloudsql/CONNECTION_NAME with no suffix, on PostgreSQL | The driver wants the full file path and was given the directory |
Ends in .s.PGSQL.5432/.s.PGSQL.5432 | The driver appends the suffix and was given it too |
| Exceeds 108 characters | The socket cannot be used from Cloud Run. Connect through a language connector or by TCP instead |
A MySQL client given the PostgreSQL form, or the reverse, fails the same way. When the service moved from one engine's sample to the other's, read the path against the engine's own page.
A worked fixture: one error, two causes#
The fixture is illustrative. The fictional service orders-api in europe-west1 connects to shop-prod:europe-west1:orders-db. orders-api-00018 takes all traffic at 09:12:30 UTC.
Incident A. The deploy came from a Terraform change that moved the service to google_cloud_run_v2_service.
| 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 |
Check 1: orders-api-00017 carries run.googleapis.com/cloudsql-instances: shop-prod:europe-west1:orders-db, and orders-api-00018 carries no such annotation. The new Terraform template has no cloud_sql_instance volume. The path in the error is correct; the socket was never mounted. Route traffic back to orders-api-00017, then add the volume and its mount to the template.
Incident B. The same deploy also replaced psycopg2 with pg8000. Both revisions list the instance.
| Time (UTC) | Revision | Error |
|---|---|---|
| 09:12:43 | orders-api-00018 | The driver reports that /cloudsql/shop-prod:europe-west1:orders-db is not a socket it can open |
Check 1 passes. Check 2: INSTANCE_UNIX_SOCKET is /cloudsql/shop-prod:europe-west1:orders-db in both revisions, matching connectionName. Check 3: psycopg2 took that value as a directory and appended .s.PGSQL.5432; pg8000's unix_sock wants the file. The configuration did not change; the code's use of it did. Fix the code to append the suffix, or roll back the release.
Where the checks mislead#
A Java application that uses the Cloud SQL Java connector can skip the built-in connection, so a missing annotation is normal for it and the error, if any, comes from the connector instead. The annotation check applies only to code that opens the socket under /cloudsql/. Connect from Cloud Run (console steps)
A socket that exists does not mean the connection behind it works. The socket appears from the revision's configuration; whether the proxy behind it can authorize against the instance depends on the service account and the Cloud SQL Admin API. If the error changes from a missing file to a refusal or a timeout, the attempt has moved to the next stage. The revision's service account lacks Cloud SQL Client
The annotation shows what the revision asked for. It does not show which instance the code read the variable from at runtime: a value loaded from a secret or computed in code can differ from the env entry the describe command prints. Trust the path in the error first.
Questions#
Why is there no /cloudsql directory in my Cloud Run container?
The revision has no Cloud SQL connection. Cloud Run mounts the socket only for instances listed in the revision's run.googleapis.com/cloudsql-instances annotation, which a YAML replace, a Terraform template or --clear-cloudsql-instances can remove.
Do I need to add .s.PGSQL.5432 to the Cloud SQL socket path?
For PostgreSQL it depends on the driver. libpq and drivers that behave like it take the directory and add the suffix; others, such as pg8000 in Google's sample, take the full file path. MySQL clients use /cloudsql/INSTANCE_CONNECTION_NAME itself.
Will rolling back fix a missing Cloud SQL socket?
When the previous revision still lists the instance and the new one does not, yes: traffic returns to a revision that mounts the socket. The next deploy from the same YAML file or Terraform template will drop it again until the file is fixed.