node-postgres: Unhandled 'error' event thrown when the connection password doesn't match

When I connect to an existing database with the connect username but the wrong password, I got an unhandled exception using the following code. The parameter err is set, but the exception is still thrown.

pg.connect(dbURL, function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  client.query('SELECT $1::int AS number', ['1'], function(err, result) {
    //call `done()` to release the client back to the pool
    done();

    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].number);
    //output: 1
    client.end();
  });
});

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 38 (20 by maintainers)

Commits related to this issue

Most upvoted comments

+1000

The same error is thrown when we pass an invalid connection:

pg.connect("bla-bla");

Other than that, it is 100% reproducible for me, for both invalid user name and invalid password.

It’s been way too long for such a critical issue to just sit here and nothing being done about it. Very frustrating.

I have a number of tests now for pg-promise commented out because of this error, which just kills jasmine framework that I’m using for testing.

Should have been labelled as a bug, at least, as it definitely is.