简体   繁体   English

Node.JS Heroku Postgres 连接不工作

[英]Node.JS Heroku Postgres Connection not working

I am trying to connect to a PostgreSQL server that I created using Heroku through Node.js in a local instance.我正在尝试在本地实例中通过 Node.js 连接到我使用 Heroku 创建的 PostgreSQL 服务器。 The issue I'm having is that the connection never gets established and I'm having trouble understanding why because there's no error output.我遇到的问题是连接永远不会建立,我无法理解为什么,因为没有错误 output。 The following is a snippet of my code:以下是我的代码片段:

async function main(){

const { Pool } = require('pg');
const pool = new Pool({
    connectionString: "postgres://very_long_url_here" || process.env.DATABASE_URL,
    ssl: {
        rejectUnathorized: false
    }
});

const client = await pool.connect();
console.log('connection established.');
const currentRecord = await client.query(`SELECT * FROM test_table WHERE email='me@aol.com'`);
console.log(currentRecord);
console.log('records pulled');
}
main();

When I run this script through the command line, none of the console.log() commands get printed, but I also don't get any errors back, although the connection clearly doesn't happen.当我通过命令行运行此脚本时,没有打印任何 console.log() 命令,但我也没有收到任何错误,尽管连接显然没有发生。 I got the connection string for the postgres database by running heroku pg:credentials:url DATABASE through the heroku CLI.我通过 heroku CLI 运行heroku pg:credentials:url DATABASE获得了 postgres 数据库的连接字符串。 Very new to node, heroku, and the stack community - I appreciate any feedback you might have!对节点、heroku 和堆栈社区非常陌生 - 感谢您提供的任何反馈!

I was having a very similar issue.我遇到了一个非常相似的问题。 It turns out that I did not freeze the node version in package.json so the problem and the local/deployed behavior was caused by running the code with 2 different node versions.事实证明,我没有冻结 package.json 中的节点版本,所以问题和本地/部署行为是由运行具有 2 个不同节点版本的代码引起的。 Locally I was running in node v10 while in heroku it was running in v14.在本地,我在节点 v10 中运行,而在 heroku 中它在 v14 中运行。 So, doing this in package.json fixed the issue:因此,在 package.json 中执行此操作解决了该问题:

"engines": { "node": "12.x" }, “引擎”:{“节点”:“12.x”},

Apparently, it does not work with v14, but the inmediate LTS version (12) seems to work fine.显然,它不适用于 v14,但中间 LTS 版本 (12) 似乎可以正常工作。 Doing a bit of research, it seems there are/have been some issues with v14 and pg Node 14.0.0 - pg does not response #2180 .做了一些研究,似乎 v14 和 pg Node 14.0.0 存在/已经存在一些问题 - pg 没有响应 #2180

the last answer worked for me, thanks最后一个答案对我有用,谢谢

in packages.json only put this;在packages.json只放这个;

"engines": { "node": "12.x" } “引擎”:{“节点”:“12.x”}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM