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:INSTANCEDB_USERandDB_PASSWORDare set from Secret ManagerDB_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
- Using direct Unix socket path:
/cloudsql/PROJECT:REGION:INSTANCE - Using TypeORM's native configuration with
hostandsocketPath - Using
pgpackage's connection string format - Verified VPC connector and Cloud SQL Auth Proxy configuration
- Confirmed all environment variables are set correctly
- 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
IP:3307: i/o timeoutmeans 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)