Nazarii Kahaniak
Nazarii Kahaniak

Reputation: 511

Cloud Run connection to Cloud SQL timeouts

I have followed the official tutorial and connected the Cloud Run instance to the Cloud SQL with Postgresql. The problem I face is that whenever the app running in the cloud run tries to access the db, it receives a timeout:

Error: connect ETIMEDOUT <port>:5432 at TCPConnectWrap.afterConnect [as incomplete]

I use express and postgraphile in my node app. My connection to the db code looks like this:

app.use(postgraphile(
    "postgres://<user>:<password>@<cloud_sql_public_ip>:5432/<db_name>",
    "public",
    {
      watchPg: true,
      graphiql: true,
      enhanceGraphiql: true,
    }),
);

Am I doing anything wrong?

Upvotes: 1

Views: 2666

Answers (2)

if you bind the CloudSQL instance to CloudRun in the UI (or via gcloud --set-cloudsql-instances=), CloudRun will make the database connection available locally. Sample Postgres URL: postgres://user@/db_name?host=/cloudsql/<the database id>&password=yourpassword. The database ID can be retrieved from the CloudSQL UI. You don't have to add IPs to allow connections but your CloudSQL instance needs a public IP.

I think you will also have to give your compute service account permissions to access your CloudSQL (roles/cloudsql.client and roles/cloudsql.instanceUser for the [email protected])

Christian

Upvotes: 0

guillaume blaquiere
guillaume blaquiere

Reputation: 75790

If you reach directly the public IP, did you authorized the Cloud Run IP to access to your database?

And because you don't know the Cloud Run IP, you have to allow 0.0.0.0/0 IP range (the whole internet), that is bad.

Have a look to the socket connection offered natively by Cloud Run

Upvotes: 3

Related Questions