Learning center

The /cloudsql socket is missing: the revision, the name or the driver path

When a Cloud Run revision reports that the socket under /cloudsql/ does not exist, compare the path in the error with the revision's Cloud SQL connections. Either the revision has no connection to that instance, the code names the instance differently, or the driver looks for the wrong file in the right directory. The first is a deploy problem that a rollback fixes; the other two live in the code or its environment variables.

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 deploy or gcloud run services update with --set-cloudsql-instances, which replaces the list, --remove-cloudsql-instances, or --clear-cloudsql-instances, which empties it. gcloud run deploy
  • gcloud 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 a cloud_sql_instance volume plus a volume_mounts entry at /cloudsql; a template without them deploys a revision without the socket. Terraform example
Previous revisionNew revisionReading
Lists the instanceLists nothing, or another instanceThe deploy dropped the connection. Route traffic back, then restore it in the file or flag that removed it
Lists the instanceLists the same instanceThe connection is in place. Go to check 2
Lists nothingLists nothingNeither 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 errorReading
Names a different project, region or instance than the annotationThe 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 connectionNameThe connection itself names the wrong instance. Fix the annotation
Matches both exactlyThe 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 triedReading
Ends in /cloudsql/CONNECTION_NAME/.s.PGSQL.5432Correct for PostgreSQL. If it is missing, check 1 or check 2 explains why
Ends in /cloudsql/CONNECTION_NAME with no suffix, on PostgreSQLThe driver wants the full file path and was given the directory
Ends in .s.PGSQL.5432/.s.PGSQL.5432The driver appends the suffix and was given it too
Exceeds 108 charactersThe 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)RevisionError
09:12:41orders-api-00018connection 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)RevisionError
09:12:43orders-api-00018The 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.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading