Heroku postgres node connection timeout

938 views Asked by At

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!

0

There are 0 answers