Pg-promise: Verify database connection or throw?

Created on 20 Jan 2016  Â·  6Comments  Â·  Source: vitaly-t/pg-promise

Hi,

Thanks for your nice package. I have a question: while writing tests, I try to check for unsuccessful connections. In a lot of situations, one can't simply assume the availability of the database, one has to test for it. The

var db = pgp(connectionObject);

doesn't seem to throw an error. It isn't a promise either. So when I put in garbage in the connectionObject, I expect to be able to test for an

assert.throws(function shouldThrow(){
var db = pgp("postgres://this:is/just:999/silly");

db.query("select '1'::int as number", true)
.then(function(result){console.log(result)})
.catch(
//re-throw error here, you know the deal
);
});

(this is probably pseudo-code by the way)

But instead of throwing, it just gives me an empty result. Any ideas on how to reliably test?

Thanks a bunch,
Rein

question

Most helpful comment

Here's a complete application for testing an invalid connection:

var options = {
    error: function (error, e) {
        if (e.cn) {
            // A connection-related error;
            console.log("CN:", e.cn);
            console.log("EVENT:", error.message);
        }
    }
};

var pgp = require("pg-promise")(options);

var db = pgp('invalid connection string');

db.connect()
    .then(function (obj) {
        obj.done(); // success, release connection;
    })
    .catch(function (error) {
        console.log("ERROR:", error.message);
    });

You would see that the same error is reported within .catch and event error. In the event you also get e.cn - secure connection details (with password hashed, if present).

For me it outputs:

CN: invalid connection string
EVENT: password authentication failed for user "Vitaly"
ERROR: password authentication failed for user "Vitaly"

All 6 comments

For that you should use connect and error events.

See how pg-promise does it when testing itself:

When there is a connection-related error, your event error will have property cn set on the context parameter. On the context object.

You can also see how this works in action, if you use pg-monitor. Here's an example.

By the way, calling this:

var db = pgp("postgres://this:is/just:999/silly");

doesn't try to open a connection. You would have to call db.connect() to make it open the connection, if this is what you want to test.

Here's a complete application for testing an invalid connection:

var options = {
    error: function (error, e) {
        if (e.cn) {
            // A connection-related error;
            console.log("CN:", e.cn);
            console.log("EVENT:", error.message);
        }
    }
};

var pgp = require("pg-promise")(options);

var db = pgp('invalid connection string');

db.connect()
    .then(function (obj) {
        obj.done(); // success, release connection;
    })
    .catch(function (error) {
        console.log("ERROR:", error.message);
    });

You would see that the same error is reported within .catch and event error. In the event you also get e.cn - secure connection details (with password hashed, if present).

For me it outputs:

CN: invalid connection string
EVENT: password authentication failed for user "Vitaly"
ERROR: password authentication failed for user "Vitaly"

Yes, I'm seeing it thrown. Thanks for the elaborate answer and example!

You are welcome!

See my latest meditation on the query performance: Performance Boost ;)

Answer to the question was later elaborated here: Verify database connection with pg-promise when starting an app

Thanks!

Den mÃ¥n 21 mar 2016 06:35Vitaly Tomilov [email protected] skrev:

Answer to the question was later elaborated here:
http://stackoverflow.com/questions/36120435/how-to-ensure-database-connection-before-starting-app-server-with-pg-promise

—
You are receiving this because you modified the open/close state.
Reply to this email directly or view it on GitHub
https://github.com/vitaly-t/pg-promise/issues/81#issuecomment-199135113

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paleite picture paleite  Â·  4Comments

normanfeltz picture normanfeltz  Â·  4Comments

blendsdk picture blendsdk  Â·  3Comments

seanh1414 picture seanh1414  Â·  4Comments

calibermind picture calibermind  Â·  3Comments