---
title: "Direct TCP to a Cloud SQL public IP from Cloud Run fails or works only sometimes | Proofline"
description: "Direct TCP to a Cloud SQL public IP needs the client's address in authorized networks, and by default Cloud Run has no fixed source address."
url: https://proofline.sh/learn/cloud-sql-direct-tcp-public-ip-cloud-run/
---

[Learning center](/learn/)

# Direct TCP to a Cloud SQL public IP from Cloud Run fails or works only sometimes

Direct TCP to a Cloud SQL public IP needs the client's address in authorized networks, and by default Cloud Run has no fixed source address. If connections work from some instances and fail from others, the allowlist holds addresses from a shared pool. If they all fail after a deploy, the revision has probably stopped routing through the VPC and Cloud NAT that gave it a static address.

Network outages The Proofline team November 23, 2026 5 min read

## How the symptom shows up

The code's host is the instance's public IP and its port is 5432: no `/cloudsql/` socket, no connector. Cloud SQL requires the client's address or range in authorized networks when a client connects directly to the public IP. [Authorized networks](https://cloud.google.com/sql/docs/postgres/authorize-networks) A client outside the list does not get a login prompt, so the failure sits at stage 1 of [the four stages](/learn/cloud-run-cloud-sql-connection-errors/#stage).

By default, Cloud Run workloads reach the internet from a regional pool of dynamic IP addresses shared across serverless workloads, and Google advises against relying on it when an external database uses an IP allowlist. A dedicated static address takes Direct VPC egress or a connector that sends all outbound traffic through the VPC, plus a Cloud NAT gateway with a reserved address. [Static outbound IP](https://cloud.google.com/run/docs/configuring/static-outbound-ip)

That gives two patterns:

-   **Intermittent**: someone added the addresses the service happened to use to the authorized networks. Instances that leave from those addresses connect; others do not.
-   **All at once after a deploy**: the service had a static NAT address, and the new revision lost the egress configuration that routed through it.

## Check 1: does the revision route through Cloud NAT?

```
for r in PREVIOUS_REVISION NEW_REVISION; do
  gcloud run revisions describe "$r" \
    --project PROJECT --region REGION \
    --format='yaml(metadata.name, metadata.annotations)'
done
```

Look for `run.googleapis.com/network-interfaces` or `run.googleapis.com/vpc-access-connector`, and `run.googleapis.com/vpc-access-egress`. [Revision template annotations](https://cloud.google.com/run/docs/reference/rest/v1/RevisionTemplate) Only `all-traffic` sends a public destination through the VPC; `private-ranges-only`, the default, sends only RFC 1918 and Private Google Access addresses there. [`--vpc-egress`](https://cloud.google.com/sdk/gcloud/reference/run/deploy)

| Previous revision | New revision | Reading |
| --- | --- | --- |
| VPC egress with `all-traffic` | No VPC egress, or `private-ranges-only` | The deploy moved traffic to the instance off the NAT address and onto the shared pool. Route traffic back, then restore the egress setting |
| VPC egress with `all-traffic` | Same | The route did not change. Check the NAT gateway's addresses against the allowlist |
| No VPC egress | No VPC egress | The service never had a fixed address. Go to check 2 |

## Check 2: read the instance's allowlist and SSL mode

```
gcloud sql instances describe INSTANCE --project INSTANCE_PROJECT \
  --format='yaml(ipAddresses, settings.ipConfiguration.ipv4Enabled, settings.ipConfiguration.authorizedNetworks, settings.ipConfiguration.sslMode)'
```

`ipv4Enabled` says whether the instance has a public address, `authorizedNetworks` lists the allowed ranges, and `sslMode` sets how SSL/TLS is enforced. [Instance resource](https://cloud.google.com/sql/docs/postgres/admin-api/rest/v1beta4/instances)

| Allowlist | Reading |
| --- | --- |
| Holds individual addresses that no NAT gateway of yours owns | They came from the shared pool. A new instance can leave from another address at any time; this explains intermittent failures |
| Holds the NAT gateway's reserved address | Correct for a service that routes all traffic through that gateway |
| Was replaced recently | `gcloud sql instances patch --authorized-networks` replaces the whole list, so an edit for another client can drop yours. Read the instance's `UPDATE` operations |

[Configure authorized networks](https://cloud.google.com/sql/docs/postgres/authorize-networks#gcloud) · [Operation types](https://cloud.google.com/sql/docs/postgres/admin-api/rest/v1/operations)

The SSL mode adds a second condition. `ALLOW_UNENCRYPTED_AND_ENCRYPTED` is the default; `ENCRYPTED_ONLY` accepts only SSL/TLS connections; `TRUSTED_CLIENT_CERTIFICATE_REQUIRED` also requires a valid client certificate. A direct connection must meet the mode itself, while connections through the Auth Proxy or the connectors are encrypted and verified automatically. [Enforce SSL/TLS](https://cloud.google.com/sql/docs/postgres/configure-ssl-instance) A connection that reaches the server and is refused for its SSL settings is a stage 3 problem with a different fix from an address that is not on the list.

## Why the built-in connection avoids this

The Cloud SQL Auth Proxy that Cloud Run embeds does not need the client's address in authorized networks: the instance needs a public IPv4 address, which does not have to be reachable from any external address. The proxy authorizes through IAM and the Cloud SQL Admin API and connects on port 3307. [Auth Proxy requirements](https://cloud.google.com/sql/docs/postgres/sql-proxy#requirements) Cloud SQL lists the connectors as the option for clients with dynamically assigned or ephemeral addresses, which fits Cloud Run. [Connection options](https://cloud.google.com/sql/docs/postgres/connect-overview#connection-type)

Moving to it is a deploy with `--add-cloudsql-instances` and a code change to the socket path, and it brings the identity requirements: Cloud SQL Client on the service account and the Admin API. [Connect from Cloud Run](https://cloud.google.com/sql/docs/postgres/connect-run) · [Which path the revision uses](/learn/which-path-cloud-run-uses-to-reach-cloud-sql/)

## A worked fixture: a shared pool and a lost NAT

The fixture is illustrative. The fictional instance `orders-db` has the public address `203.0.113.20` and `sslMode: ENCRYPTED_ONLY`; the code of `orders-api` connects there with SSL.

**Outcome A: intermittent.** No revision of `orders-api` has VPC egress. `authorizedNetworks` holds `198.51.100.14/32` and `198.51.100.87/32`, added in October under the name `orders-api-egress`. Since 06:00 UTC, about a third of new instances fail their first connection; the rest connect. Nothing was deployed. The two addresses were observed from the shared pool; new instances leave from addresses the list does not contain. Give the service a static address through Direct VPC egress, `all-traffic` and Cloud NAT, then replace the two entries with the NAT address. Or move to the built-in connection.

Outcome B: after a deploy.

| Revision | `network-interfaces` | `vpc-access-egress` | New instance connects? |
| --- | --- | --- | --- |
| `orders-api-00017` | `shop-vpc` / `run-ew1` | `all-traffic` | Yes (09:20:02, tag `prev`) |
| `orders-api-00018` | `shop-vpc` / `run-ew1` | `private-ranges-only` | No (from 09:12:41) |

`authorizedNetworks` holds `198.51.100.200/32`, the NAT gateway's reserved address. `orders-api-00018` was deployed at 09:12:30 UTC with `--vpc-egress private-ranges-only`, so its traffic to `203.0.113.20` no longer enters the VPC and leaves from the shared pool. Route traffic back to `orders-api-00017` and restore `all-traffic` in the deploy.

## Where the checks mislead

An instance that connects once proves only that its current address is allowed. With the shared pool, a fresh instance of the previous revision may connect or fail by chance, so one start does not split deploy from environment here; the revision's egress settings do.

Direct VPC egress with Cloud NAT can add 30 seconds or more of startup delay; Google suggests connectors for better startup performance with Cloud NAT. A client with a short connect timeout can fail on that delay even when the address is allowed. [Direct VPC egress limitations](https://cloud.google.com/run/docs/configuring/vpc-direct-vpc#limitations)

The authorized networks list applies to direct connections. It plays no part when the same instance is also reached through the Auth Proxy, so another client that works does not show the list is correct for this one.

## Questions

### Does Cloud Run have a fixed outbound IP address for Cloud SQL authorized networks?

Not by default. Cloud Run uses a shared regional pool of dynamic addresses. A static address requires Direct VPC egress or a connector with `all-traffic` egress and a Cloud NAT gateway with a reserved address.

### Why does my Cloud Run service connect to Cloud SQL only sometimes?

On a direct public IP connection, the usual reason is an allowlist of addresses taken from the shared pool. Instances that leave from a listed address connect and the rest do not.

### Do I need authorized networks when Cloud Run uses the Cloud SQL connection?

No. The built-in connection goes through the Cloud SQL Auth Proxy, which authorizes with IAM. No address needs to be added to authorized networks for it.

## 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](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Which path does this Cloud Run revision use to reach Cloud SQL?](/learn/which-path-cloud-run-uses-to-reach-cloud-sql/)
    
    Network outages The Proofline team November 23, 2026 6 min read
    
-   ### [Cloud Run cannot reach Cloud SQL: revision, identity or connection?](/learn/cloud-run-cloud-sql-connection-errors/)
    
    Broken dependencies The Proofline team November 23, 2026 9 min read
