I am trying to establish a simple connection from a NodeJs app to my Postgres local database. Here is the content of my node code index2.js
var fs = require('fs');
var path = require('path');
var PGPASS_FILE = path.join(__dirname, "./.pgpass");
var pgtokens = fs.readFileSync(PGPASS_FILE).toString().trimRight().split(":");
var host = pgtokens[2];
var port = pgtokens[3];
var dbname = pgtokens[4];
var user = pgtokens[0];
var password = pgtokens[1];
var conString = "postgres://"+user+":"+password+"@"+host+":"+port+"/"+dbname;
var pg = require('pg.js');
var Sequelize = require('sequelize');
var sequelize = new Sequelize(dbname, user, password,{
dialectModulePath:"pg.js",
dialect: "postgres",
port: 5432
});
sequelize
.authenticate()
.complete(function (err) {
if (!err) {
console.log('Unable to connect to the database', err);
} else {
console.log('Connection has been establised succesfully!');
}
})
I am using the module pg.js not pg to connect to Postgres and I have tested that it works.
My problem is with Sequelize. The error I get is the following:
c:\psql-node\node-modules\sequelize\lib\transaction-manager.js:10
throw new Error("The dialect + sequelize.getDialect()+" is not support
^
Error: The dialect postgres is not suppported.
at new module.exports (c:\psql-node\node_modules\sequelize\lib\transaction-manager.js:10:11)
at new module.exports.Sequelize (c:\psql-node\node_modules\sequelize\lib\sequelize.js:128:31)
at Object.<anonymous> (c:\psql-node\index2.js:16:17)
etc...
To be honest, I'm not sure if how I tell Sequelize to use 'pg.js' is correct, that's the line dialectModulePath:"pg.js"
Any ideas?
Edit: Thanks to @peter-lyons I found out a bit more about the issue: The error I get is almost the same but before it indicates:
[Error: Cannot find module 'pg/lib/connection-parameters'] code: 'MODULE_NOT_FOUND'
which is normal as the path to it should be pg.js/lib/connection-parameters
Any idea how I modify node_modules\sequelize\lib\sequelize.js so it gets the right file?