简体   繁体   中英

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. 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. 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. I got the connection string for the postgres database by running through the heroku CLI.获得了 postgres 数据库的连接字符串。 Very new to node, heroku, and the stack community - I appreciate any feedback you might have!

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. Locally I was running in node v10 while in heroku it was running in v14. So, doing this in package.json fixed the issue:

"engines": { "node": "12.x" },

Apparently, it does not work with v14, but the inmediate LTS version (12) seems to work fine. 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 .

the last answer worked for me, thanks

in packages.json only put this;

"engines": { "node": "12.x" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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