0
votes

This is my Google Cloud Function:

async function getPSQLdata() {
  const pg = require('pg');

  var pgConfig = {
    user: 'postgres',
    password: '[MY PASSWORD]',
    database: '[MY DB NAME]',
    host: '[PUBLIC IP ADDRESS OF DB INSTANCE]'
  };

  var pgPool;

  if (!pgPool) {
    pgPool = new pg.Pool(pgConfig);
  }

  const scores = await pgPool
    .query("SELECT * from table")
    .then(res => console.log(res.rows[0]))
    .catch(e => console.error(e.stack));
}

exports.dailyFill = async function main() {
  await getPSQLdata();
};

Google Cloud Logging says: "Function execution took 60003 ms, finished with status: 'timeout'".

What I have tried so far:

  • I have enabled the Cloud SQL Admin API within the GCP project
  • I have enabled the Cloud SQL API within the GCP project
  • I have added the Google Cloud Function service account to the IAM as the roles "Cloud SQL Client" and "Project Editor"
1
your function should return a promise otherwise it will just timeout. change last statement in getPSQLdata to return pgPool.query("SELECT * from table").then(res =>console.log(res.rows[0])).catch(e => console.error(e.stack)); and export this function as in exports.dailyFill = getPSQLdataMethkal Khalawi
Thx. What do you mean by " export this function as in exports.dailyFill = getPSQLdata"?JShinigami

1 Answers

0
votes

Cloud functions provides a way you can connect to the DB via Cloud SQL Proxy, see details here https://cloud.google.com/sql/docs/mysql/connect-functions#public-ip-default_1 this is the preferred method.

The key difference is instead of defining host you would use the following

socketPath: '/cloudsql/INSTANCE_CONNECTION_NAME'