简体   繁体   English

如何从 Cloud Functions 连接到 Google Cloud SQL (PostgreSQL)?

[英]How to connect to Google Cloud SQL (PostgreSQL) from Cloud Functions?

I feel like I've tried everything.我觉得我什么都试过了。 I have a cloud function that I am trying to connect to Cloud SQL (PostgreSQL engine).我有一个云 function,我正在尝试连接到云 SQL(PostgreSQL 引擎)。 Before I do so, I pull connection string info from Secrets Manager, set that up in a credentials object, and call a pg (package) pool to run a database query.在此之前,我从 Secrets Manager 中提取连接字符串信息,在凭据 object 中进行设置,然后调用 pg(包)池来运行数据库查询。

Below is my code: Credentials:下面是我的代码:凭据:

import { Pool } from 'pg';

const credentials: sqlCredentials = {
   "host":"127.0.0.1",
   "database":"myFirstDatabase",
   "port":"5432",
   "user":"postgres",
   "password":"postgres1!"
}

const pool: Pool = new Pool(credentials);

await pool.query(`select CURRENT_DATE;`).catch(error => console.error(`error in pool.query: ${error}`));

Upon running the cloud function with this code, I get the following error:使用此代码运行云 function 后,出现以下错误:

error in pool.query: Error: connect ECONNREFUSED 127.0.0.1:5432

I have attempted to update the host to the private IP of the Cloud SQL instance, and also update the host to the Cloud SQL instance name on this environment, but that is to no avail.我已尝试将主机更新为 Cloud SQL 实例的私有 IP,并将主机更新为 Cloud SQL 实例名称,但在此环境下无效。 Any other ideas?还有其他想法吗?

Through much tribulation, I figured out the answer.历经千辛万苦,终于找到了答案。 Given that there is NO documentation on how to solve this, I'm going to put the answer here in hopes that I can come back here in 2025 and see that it has helped hundreds.鉴于没有关于如何解决这个问题的文档,我将把答案放在这里,希望我能在 2025 年回到这里,看看它已经帮助了数百人。 In fact, I'm setting a reminder in my phone right now to check this URL on November 24, 2025.事实上,我现在正在手机中设置提醒,以便在 2025 年 11 月 24 日检查这个 URL。

Solution: The host must be set as:解决方法:主机必须设置为:

/cloudsql/<googleProjectName(notId)>:<region>:<sql instanceName>

Ending code:结束代码:

import { Pool } from 'pg';

const credentials: sqlCredentials = {
   "host":"/cloudsql/my-first-project-191923:us-east1:my-first-cloudsql-inst",
   "database":"myFirstDatabase",
   "port":"5432",
   "user":"postgres",
   "password":"postgres1!"
}

const pool: Pool = new Pool(credentials);

await pool.query(`select CURRENT_DATE;`).catch(error => console.error(`error in pool.query: ${error}`));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Google Cloud Functions NodeJS连接到Google云端存储 - How to connect to Google Cloud Storage from Google Cloud Functions NodeJS 如何从IBM Cloud Functions连接PostgreSQL的Compose? - How to Connect Compose for PostgreSQL from IBM Cloud Functions? Google Cloud Platform 使用内部 DNS 名称从 Cloud Functions 连接到 PostgreSQL 服务器 - Google Cloud Platform Connect to PostgreSQL Server from Cloud Functions Using Internal DNS Name 谷歌云函数连接到mongodb - google cloud functions connect to mongodb 将节点js连接到postgresql(谷歌云平台) - connect node js to postgresql (google cloud platform) NodeJ:无法从Google App Engine连接Google Cloud SQL - NodeJs:Unable to connect Google Cloud SQL from Google App Engine 无法使用私有 IP 从 Cloud Functions 连接到 Cloud SQL SQL Server - Cannot connect to Cloud SQL SQL Server from Cloud Functions using private IP 如何使用内部IP从Google Cloud Functions连接到我的Compute Engine MongoDB实例? - How to connect to my Compute Engine MongoDB instance from Google Cloud Functions using the internal IP? 从Node.JS连接到Google Cloud SQL的正确方法 - Right way to connect to Google Cloud SQL from Node.JS 在 Google Cloud SDK 的代码设置中部署 Google Cloud 函数 - Deploy Google Cloud Functions with in Code Settings from Google Cloud SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM