我最近开始使用谷歌云,我想将我的Postgres数据库托管到谷歌云,并将我的node.js应用程序部署到应用程序引擎,但我在将我的nodejs应用程序连接到托管数据库时遇到了问题,我已经启用了云SQL API。我使用nodepostgres这里是我的连接对象:
const { Pool } = require("pg");
const pool = new Pool({
user: process.env.DB_USER,
password: process.env.DB_PASS,
port: 5432,
host: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
database: process.env.DB_NAME,
});
module.exports = pool;
这是一个查询:
router.get("/", async (req, res) => {
try {
const { rows } = db.query("SELECT * FROM users ORDER BY id DESC");
res.json(rows);
} catch (err) {
console.error(err);
}
});
这是我的应用程序
runtime: nodejs14
env_variables:
DB_USER: postgres
DB_PASS: password here
DB_NAME: the db name here
CLOUD_SQL_CONNECTION_NAME: my connection name here
beta_settings:
cloud_sql_instances: my connection name here
当我运行这个时,我得到一个ENONET错误:
UnhandledPromiseRejectionWarning: Error: connect ENOENT /cloudsql/instance_id/.s.PGSQL.5432
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:121478) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:121478) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我不确定到底发生了什么,我遵循了官方文件,但仍然没有成功。
我认为您的问题是在您的连接对象中,字段"主机";需要一个IP地址,而它看起来像是在输入文档中某些页面中提到的UNIX套接字名称。请尝试输入您的云SQL实例的有效公共或私有IP地址,因为在这种情况下,您使用的库就是这样期望的。
此外,请确保您在每种情况下都正确配置了应用程序引擎(公共或私人IP(,如本文档页面中所示