Error connecting to a postgres db with nodejs sequelize pg.js "the dialect postgres is not supported"

StackOverflow https://stackoverflow.com/questions/23331897

Question

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?

Was it helpful?

Solution

So in theory your snippet should work, but the sequelize code is throwing an error that I suspect is misleading. You'll need to edit the lib/transaction-manager.js file because it is swallowing the error details you need. Add a console.error(err) in the catch block before the throw to make sure that error is not something else weird like your install of sequelize is missing files or not dealing with windows paths correctly or something like that. Make that one change, rerun your snippet, and update your question with the details if that doesn't lead you to a root cause and a solution directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top