Postgresql - 由于连接超时错误连接终止

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

我在谷歌云中创建了一个谷歌云函数,它将连接到我在谷歌云中创建的 postgresql 实例。

我正在使用“pg”节点模块。

我为此创建了一个私有IP。

我收到以下错误:

错误:由于连接超时而终止连接 Timeout.connectionTimeoutHandle.setTimeout (/workspace/node_modules/pg/lib/client.js:106:28) 超时 (timers.js:436:11) 在 tryOnTimeout (timers.js:300:5) 在 listOnTimeout (timers.js:263:5) 在 Timer.processTimers (timers.js:223:10)

当尝试查询谷歌云中的数据库时。

这是我在谷歌云功能中使用的配置。

{ “主持人”: ””, “用户”:“”, “密码”:“”, “db”:“”, “端口”:“5432”, “表”:“

请帮我解决这个问题

postgresql google-cloud-platform cloud vpc node-postgres
3个回答
4
投票

升级 pg npm 版本解决了该问题。

"pg": "^7.3.0",

"pg": "^8.7.1",

如果问题仍然存在,请检查您的节点版本。将节点升级到 >=14。


1
投票

根据官方文档:

从 Cloud Functions 连接到 Cloud SQL

要直接使用私有IP连接,您需要:

1.确保上面创建的Cloud SQL实例有私有IP 地址。如果需要添加,请参阅配置私有 IP 页面 获取说明。

2.在同一个目录中创建 Serverless VPC Access 连接器 VPC 网络作为您的 Cloud SQL 实例。除非您使用共享 VPC、连接器必须与连接器在同一个项目、同一个地域 使用它的资源,但连接器可以将流量发送到资源 不同地区。

3.配置Cloud Functions以使用连接器。使用您的连接 实例的私有IP和端口5432。

4.使用实例的私有IP和端口5432进行连接

您还可以找到建立数据库连接的 Node js 代码:

const connectWithTcp = config => {
  // Extract host and port from socket address
  const dbSocketAddr = process.env.DB_HOST.split(':'); // e.g. '127.0.0.1:5432'

  // Establish a connection to the database
  return Knex({
    client: 'pg',
    connection: {
      user: process.env.DB_USER, // e.g. 'my-user'
      password: process.env.DB_PASS, // e.g. 'my-user-password'
      database: process.env.DB_NAME, // e.g. 'my-database'
      host: dbSocketAddr[0], // e.g. '127.0.0.1'
      port: dbSocketAddr[1], // e.g. '5432'
    },
    // ... Specify additional properties here.
    ...config,
  });
};

0
投票

出现同样的错误

错误:postgresql 连接由于连接超时而终止

pool = new Pool({
            max: 300,
            connectionTimeoutMillis: 5000,
            host: 'c-abxxxfg.22ps25nxx7gjw5.postgres.cosmos.azure.com',
            port: 5432,
            user: 'citus',
            password: 'xxxxxxxx',
            database: 'citus',
            ssl: true,
        });

有人可以帮忙吗?

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