Search code examples
node.jsgoogle-cloud-sqltypeormcloud-sql-proxygoogle-cloud-run

Unable to connect CloudRun service to CloudSQL instance ( Nodejs-Express-Typeorm ) server


I'm having issues connecting my node-express-typeorm server with CloudSQL Postgres instance using CloudRun.

I have successfully added the database proxy based on the official doc: https://cloud.google.com/run/docs/configuring/connect-cloudsql

And set environment variable for the express services as

TYPEORM_URL=/cloudsql/[CONNECTION NAME]

But the app is failing to start as its unable to connect to the database.


Solution

  • I have solved the issue. The culprit was TypeORM. TypeORM is parsing database host from TYPEORM_URL using / delimiter. And it was only reading cloudsql instead of /cloudsql/[CONNECTION NAME]. So the app was unable to connect to the correct endpoint.

    To fix the issue set the following environment variables instead of TYPEORM_URL

    TYPEORM_HOST=/cloudsql/[CONNECTION NAME]
    
    TYPEORM_USERNAME=<username>
    
    TYPEORM_PASSWORD=<password>
    
    TYPEORM_DATABASE=<db>