2

I'm trying to connect to a Postgres database from my Heroku node app, which works when running locally, both through node and by running the heroku local web command, but when running it on Heroku, it times out while waiting for pool.connect

I'm running the following code snippet through the Heroku console (I've also tried using this code in my app directly, but this is more efficient than redeploying each time):

node -e "
    const { Pool } = require('pg');

    const pool = new Pool({
    connectionTimeoutMillis: 15000,
      connectionString: process.env.DATABASE_URL + '?sslmode=require',
      ssl: {
        rejectUnauthorized: true
      }
    });

    console.log('pool created');
    (async() => {

     try {
        console.log('connecting');
        const client = await pool.connect(); // this never resolves
        console.log('querying');
        const { rows } = await client.query('SELECT * FROM test_table LIMIT 1;');
        console.log('query success', rows);
        client.release()
      } catch (error) {
        console.log('query error', error);
      }
    })()
"

Things I've tried so far:

  • Using the pg Clientinstead of Pool
  • Using ssl: true instead of ssl: { rejectUnauthorized: true }
  • Using client.query without using pool.connect
  • Increased and omitted connectionTimeoutMillis (it resolves quickly when running locally since I'm querying a database that has just one row)
  • I've also tried using callbacks and promises instead of async / await
  • I've tried setting the connectionString both with the ?sslmode=require parameter and without it
    • I have tried using pg versions ^7.4.1 and ^7.18.2 so far

My assumption is that there is something I'm missing with either the Heroku setup or SSL, any help would be greatly appreciated, Thanks!

enzoborgfrantz
  • 468
  • 3
  • 11
  • If changing to v7 of `node-postgres` fixes it for you, then the problem is due to the v8 changes, [as listed in the changelog](https://github.com/brianc/node-postgres/blob/master/CHANGELOG.md#pg800). – vitaly-t Apr 24 '20 at 13:40
  • I have tried using pg versions `^7.4.1` and `^7.18.2` so far, will update the description – enzoborgfrantz Apr 24 '20 at 13:46

0 Answers0