0

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='[email protected]'`);
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 heroku pg:credentials:url DATABASE through the heroku CLI. Very new to node, heroku, and the stack community - I appreciate any feedback you might have!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.