0
votes

I have node Js code below which connects to PostgreSql db using connection pool:

const express = require("express");
const path = require("path");
var bodyParser = require("body-parser");
var cors = require("cors");

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());

const Pool = require("pg").Pool;

const pool = new Pool({
  user: "",
  host: "",
  database: "",
  password: "",
  port: 5432,
});

app.get("/get_que", (req, res) => {
  pool.query(
    "SELECT json FROM surveys where available = TRUE;",
    (error, results) => {
      if (error) {
        console.log(error);
      }
      res.json({ data: results.rows });
    }
  );
});

Above app is deployed in Google Cloud Run and configured Google Cloud SQL (PostgresSql connection), but unable to connect from Google Cloud Run app.

Using Cloud SQL proxy in local, I am able to connect to cloud sql instance.

I tried public ip of the Cloud SQL instance in the "host" parameter in the above app, it is not connecting,

How to connect to Google Cloud SQL instance from the above NodeJs app which is deployed on Google Cloud Run?.

1
You need to enable to public IP of the Cloud SQL and setup a connection via the Cloud Run connections tab. Also after that you need to connect using unixsockets /cloudsql/<instance_id>Pentium10
I have attached Cloud SQL instance to cloud run while deploying the revision. Now, in my code , in the host parameter i need to put '\cloudsql\<instance_id>'?Jazzusg
const Pool = require("pg").Pool; //Below is for Google cloud const pool = new Pool({ user: 'postgres', host: '/cloudsql/<instance connection name>/.s.PGSQL.5432', database: 'db1', password: 'password123' //port: 5432, }); tried above host parameter , also added 'SQL Client' role to service account. still not workingJazzusg

1 Answers

2
votes

Even though the public IP is enabled in the Cloud SQL instance by default no networks are allowed. See the adding authorized networks.

You have a very detailed guide in Connecting from Cloud Run.