0

I am trying to query a table named "client" of my Cloud SQL database.I am using cloud functions to access Cloud SQL. When I make the query, I get the following error:

relation "client" does not exist

I want to list all the clients in my table. When I run the same command from psql in cloud shell, I get the correct result:

postgres=> SELECT * FROM client LIMIT 2 ;
 numcli |    nom    |   prenom   |    ville    |    tel
--------+-----------+------------+-------------+------------
      0 | Ernaut    | Bernadette | marseille   | 0296645394
      1 | Christian | Louis      | montpellier | 0417103362
(2 rows)
postgres=>

But the following code in Cloud Functions encounters an error:

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import * as pg from "pg";

export const sendMoney = functions.https.onCall(async (data, context) => {
  console.log(data);
  if (!context.auth || !context.auth.uid)
    throw new functions.https.HttpsError(
      "unauthenticated",
      "L'action est refusée car aucun utilisateur n'est connecté"
    );

  const pgConfig = {
    max: 1,
    user: "postgres",
    password: "******",
    database: "pay",
    host: "/cloudsql/******:us-central1:****"
  };
  let pgPool;
  if (!pgPool) {
    pgPool = new pg.Pool(pgConfig);
  }
  return pgPool
    .query("SELECT * FROM client LIMIT 2")
    .then(result => {
      console.log(" \n RESULT \n");
      console.log(result);
      };
    })
    .catch(e => {
      console.log(e);
    });
});
5
  • Can you post the command you are running from Cloud Shell to get the correct result? Commented Apr 27, 2019 at 21:37
  • i run the same command in the cloud shell and the cloud functions. These are the results postgres=> SELECT * FROM client LIMIT 2 ; Commented Apr 27, 2019 at 22:26
  • Is it possible that you've accidentally connected to the wrong database? What commands do you use to connect? What are the results of SELECT table_name FROM information_schema.tables WHERE table_schema='public' from inside the function? Commented Apr 28, 2019 at 2:44
  • @kurtisvg I just realized that I was connecting to a database that did not contain any tables or data. When I run the command in cloud shell, by default the command is started on the postgres database. That's why it worked. And in cloud functions I connected to a database called "test", which contained nothing.Thanks for the help Commented Apr 28, 2019 at 10:16
  • No problem! Please make sure to mark your answer as accepted to help other users resolve the issue more quickly in the future. Commented Apr 28, 2019 at 20:23

2 Answers 2

1

The problem was I was connecting to the default database (postgres) when connecting from Cloud Shell. When connecting from Cloud Functions, I was connecting to the pay database, which didn't have any tables.

I updated this config:

  const pgConfig = {
        max: 1,
        user: "postgres",
        password: "******",
        database: "pay",
        host: "/cloudsql/******:us-central1:****"
      };

To connect to the "default" database instead:

const pgConfig = {
    max: 1,
    user: "postgres",
    password: "******",
    database: "postgres",
    host: "/cloudsql/******:us-central1:****"
  };

And then everything worked correctly.

Sign up to request clarification or add additional context in comments.

Comments

0

Confirm the data: SELECT * FROM name--; Check url for any doubt about how to connect to cloud SQL. https://cloud.google.com/sql/docs/postgres/quickstart

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.