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
Client
instead ofPool
- Using
ssl: true
instead ofssl: { rejectUnauthorized: true }
- Using
client.query
without usingpool.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
- I have tried using pg versions
My assumption is that there is something I'm missing with either the Heroku setup or SSL, any help would be greatly appreciated, Thanks!