[英]How can I get data from a Heroku PostgreSQL database from a Google Cloud Function?
Google does not offer a free PostgreSQL instance, but Heroku does. Google 不提供免费的 PostgreSQL 实例,但 Heroku 提供。 Because of that, I am wanting to use GCP for everything except the database.因此,我想将 GCP 用于除数据库之外的所有内容。 Is there any way to connect to a Heroku database from a Google Cloud Function?有什么方法可以从 Google Cloud Function 连接到 Heroku 数据库?
I am new to Google Cloud and am familiar with creating and hosting Express apps.我是 Google Cloud 新手,熟悉创建和托管 Express 应用程序。 I know that with Express, packages like Massive can be used to establish connections like this:我知道使用 Express,可以使用像 Massive 这样的包来建立这样的连接:
const express = require("express");
const massive = require("massive");
const { CONNECTION_STRING } = process.env;
const app = express();
app.use(express.json());
massive({
connectionString: CONNECTION_STRING,
ssl: { rejectUnauthorized: false },
})
.then((databaseInstance) => {
app.set("database", databaseInstance);
console.log(gradientString.pastel("DB Connected"));
})
.catch((error) => {
console.log(error);
});
but because there is no server, I am not sure how I would go about calling a Heroku database from a Cloud Function.但是因为没有服务器,我不确定如何从 Cloud Function 调用 Heroku 数据库。 Is that possible?那可能吗?
It is not much different in cloud functions.在云功能上并没有太大的不同。 In the package.json file add dependency to massive:在 package.json 文件中添加对海量的依赖:
"dependencies": {
"massive": "^6.10.2"
}
Then follow instructions from Heroku to get the connection string of postgres and use that in the code you shared.然后按照Heroku的说明获取 postgres 的连接字符串,并在您共享的代码中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.