According to the node -dbi code , the pg adapter builds the connection url as
var cfg = this._connectionParams; if (connectionParams["dsn"]) { this._pgUrl = connectionParams["dsn"]; } else { this._pgUrl = 'pg://'+cfg.user+':'+cfg.password+'@'+cfg.host+'/'+cfg.database; } this._dbClient = new pg.Client(this._pgUrl);
Seeing the error message
ERROR: connect: Error: connect ECONNREFUSED 10.0.97.135:5432
your code tries to connect to the default postgres port, 5432, which hints that the port is not properly redirected to pg. The source code for node -dbi does not use the cfg.port variable, so this may explain your error, since the pg library is never provided with port 34607, which your postgres instance is supposed to listen on, if we believe in the ENV variable, is displayed in your message.
Note that node-dbi prefers dsn when it exists:
this._pgUrl = connectionParams["dsn"];
You seem to have a dsn in ENV called 'url'.
It's hard to give you an accurate fix outside, but I believe that it should work with
var DBWrapper = require('node-dbi').DBWrapper; var pgconf = vcap_services['postgresql'][0].credentials; var dbConnectionConfig = { dsn: pgconf.url }; var dbWrapper = new DBWrapper('pg', dbConnectionConfig); dbWrapper.connect();
Update
This solution results in an error.
OUT ERROR: connect: Error: getaddrinfo ENOTFOUND undefined undefined:5432
which shows that dsn is not given or analyzed accordingly on pg . This led me to the conclusion that node-dbi uses a very old version of pg . Therefore, we should not try to use the connection parameter as a string.
Do you confirm that the version of node-dbi you are using is 0.7.1?
I just installed node-dbi . It gave me 0.7.1 where I can read
var cfg = this._connectionParams; this._pgUrl = 'pg://' + cfg.user + ':' + cfg.password + '@' + cfg.host + '/' + cfg.database; this._dbClient = new pg.Client(this._pgUrl);
therefore, this code does not match what we see on github. In this version, dsn is not processed.
This can be confirmed using https://github.com/DrBenton/Node-DBI/commits/master . Version 0.7.1 dates from January 2014, and postgres dsn processing dates back to January 2015, so it has not yet been published ...
There is a mismatch in your log, and I donβt understand why it indicates that the host is undefined, so I will bypass it with uri directly, since uri seems to be correct.
Seeing how node-dbi builds a pg connection string, you can try
var url = require('url'); var DBWrapper = require('node-dbi').DBWrapper; var pgconf = vcap_services['postgresql'][0].credentials; console.log(pgconf.uri); var parts = url.parse(pgconf.uri); var dbConnectionConfig = { user: parts.auth.split(':')[0], password: parts.auth.split(':')[1], host: parts.host, database: parts.pathname.substr(1) }; var dbWrapper = new DBWrapper('pg', dbConnectionConfig); dbWrapper.connect();
Once you earn it, it is probably time to wonder why node-dbi seems so "fragile" and uses another database wrapper, such as sequelize , if you do not want to use pg directly.
Then he must work with
var sequelize = new Sequelize(pgconf.uri, {})
according to http://docs.sequelizejs.com/en/latest/api/sequelize/