0

Environment

  • Google Cloud Run
  • Cloud SQL PostgreSQL instance
  • Node.js 18
  • TypeORM
  • Cloud SQL Auth Proxy (running in same container)

Current Setup

My Cloud Run service is configured with:

  • VPC Connector: service-connector
  • VPC Egress: all-traffic
  • Cloud SQL connection: Added via --add-cloudsql-instances

Database configuration (TypeORM):

const config = {
  type: 'postgres',
  url: `postgresql://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}/${process.env.DB_NAME}`,
  extra: {
    idleTimeoutMillis: 60000,
    statement_timeout: 60000,
    connectionTimeoutMillis: 60000,
    keepAlive: true,
    ssl: false
  }
};

Environment variables:

  • DB_HOST=/cloudsql/PROJECT:REGION:INSTANCE
  • DB_USER and DB_PASSWORD are set from Secret Manager
  • DB_NAME=***

Issue

The Cloud Run service fails to connect to the Cloud SQL instance. The logs show:

failed to connect to instance: error Dialing via SQL Proxy Client: Dial error: failed to dial (connection name = "PROJECT:REGION:INSTANCE"): dial tcp IP:3307: i/o timeout

What I've Tried

  1. Using direct Unix socket path: /cloudsql/PROJECT:REGION:INSTANCE
  2. Using TypeORM's native configuration with host and socketPath
  3. Using pg package's connection string format
  4. Verified VPC connector and Cloud SQL Auth Proxy configuration
  5. Confirmed all environment variables are set correctly
  6. Verified service account has necessary permissions

Question

What is the correct way to configure TypeORM to connect to Cloud SQL PostgreSQL from Cloud Run using Unix Domain Socket? The documentation examples seem to focus on MySQL, and the PostgreSQL examples often use TCP connections instead of Unix sockets.

Additional Context

  • The same configuration works locally using Cloud SQL Auth Proxy
  • The service account has the necessary IAM roles
  • The Cloud SQL instance is in the same region as the Cloud Run service
1
  • The IP:3307: i/o timeout means it is a networking issue, the Cloud SQL Proxy is unable to connect to the Cloud SQL instance. This points at the VPC connector maybe not being configured properly? Is it for the same VPC network as the Cloud SQL instance, is it in the same region? You may also want to double-check that your VPC network firewall rules allow TPC connections on port 3307 of your Cloud SQL instance private IP (Proxy server port) Commented Feb 25 at 17:23

1 Answer 1

0

In my case I used the native Unix socket connector with the volume and had to specify the db URL like this:

`postgresql://${DB_USER}:${DB_PASSWORD}@/${DB_NAME}?host=${DB_HOST}/.s.PGSQL.5432`

Where DB_HOST is like your /cloudsql/PROJECT:REGION:INSTANCE

Don't ask how long it took to get it working...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.