如何用postgresql解决云函数错误和云sql错误关系“table_name”不存在

问题描述 投票:0回答:2

我正在尝试查询我的Cloud SQL数据库中名为“client”的表。我正在使用云功能来访问Cloud SQL。当我进行查询时,出现以下错误:

relation "client" does not exist

我想列出我表中的所有客户端。当我从cloud shell中的psql运行相同的命令时,我得到了正确的结果:

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

但是Cloud Functions中的以下代码遇到错误:

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);
    });
});
node.js postgresql google-cloud-functions google-cloud-sql
2个回答
1
投票

问题是我从Cloud Shell连接时连接到默认数据库(postgres)。从Cloud Functions连接时,我连接到pay数据库,该数据库没有任何表。

我更新了这个配置:

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

要改为连接到“默认”数据库:

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

然后一切正常。


0
投票

确认数据:SELECT * FROM name--;检查网址是否有关于如何连接到云SQL的任何疑问。 https://cloud.google.com/sql/docs/postgres/quickstart

© www.soinside.com 2019 - 2024. All rights reserved.