简体   繁体   English

如何从 Google Cloud Function 的 Heroku PostgreSQL 数据库中获取数据?

[英]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.

相关问题 如何从 dart 代码连接到云 PostgreSQL 数据库? - How can I connect to a Cloud PostgreSQL database from dart code? 我们如何从Postgresql中的数据库功能将数据写入文件? - how can we write data to a file from database function in postgresql? 无法从 herokuCLI 连接到 heroku 上的 postgreSQL 数据库 - can not connect to postgreSQL database on heroku from herokuCLI 如何从 Express 连接到 Heroku PostgresQL 数据库? - How to connect to Heroku PostgresQL database from Express? 如何从 Heroku PostgreSQL 数据库创建 ERD? - How to create ERD from Heroku PostgreSQL database? 如何从heroku上托管的PostgreSQL数据库下载Schema? - How can I download the Schema from a PostgreSQL db hosted on heroku? 当我通过云 Function (Node.js) 传递数据时,如何从 Flutter 应用程序中的 PostgreSQL 中的选定行获取数据? - How do I get the data from selected rows in PostgreSQL from Flutter app when I'm passing it through Cloud Function (Node.js)? 如何从heroku中的postgresql获取表? - How to get tables from postgresql in heroku? 如何在没有数据的情况下将表结构从本地 PostgreSQL 数据库推送到 Heroku 数据库 - How to push table structure from local PostgreSQL database to Heroku database without data 无法使用 knex 从 postgresql 数据库获取数据 - Can't get data from postgresql database using knex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM