0

I am new with postgres. I have a app which is running fine on local with same env configs but on staging on making the first i.e login request it gives me request timeout error as below.

at=error code=H12 desc="Request timeout" method=POST path="/login" host=abc.herokuapp.com request_id=87ed82a9-799f-415d-8277-bdb195978570 fwd="XX.XX.XX.XXX" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0 protocol=https

I am using Node.js for backend, npm pg package to connect with postgres and deployed the app on heroku.

here is the connection file.

const {Pool} = require('pg')

const dbConfig = {
    connectionString: process.env.DATABASE_URL,
    ssl:true,
  }

console.log('process.env.DATABASE_URL: '+process.env.DATABASE_URL);

const pool = new Pool(dbConfig);
pool.on('error',function(err){
    console.log('Error Message'+err.message);
    console.log('Error Stack'+err.stack);
})

module.exports = {
    pool,
}

The request login handler is as follows:

const { pool } = require('../db/dbConfig');


router.post('/login', async (req, res) => {
    try {
        const { email, password } = req.body;

        if (!email || !password) {
            errors.push({ msg: 'Please enter all fields' });
            res.render('login', { errors });
        }
        console.log("going to query pool"); // gets printed
        const loginResult = await pool.query('SELECT Id, Name, email, FROM users WHERE email = $1 AND password2__c = $2', [email, password]);
        console.log("result"); // this never gets printed
        res.render('dashboard', { user: { name: "aa", token: "abc" } });
    } catch {
        res.render('error')
    }
})

On local it is running fine and the response is received but on production it is giving timeout.

Digvijay Rathore
  • 637
  • 1
  • 6
  • 21

1 Answers1

0

If you can connect, it's probably an issue with the way that the postgresql library is being initialized. If you're using the latest "node-postgres" ("pg"), make sure that you have ssl rejectUnauthorized set to false:

const { Pool } = require('pg');
const pool = new Pool({
    connectionString: process.env.DATABASE_URL,
    ssl: { rejectUnauthorized: false }
});
user256173
  • 69
  • 1
  • 1
  • 9