如何在connect-pg-simple中指定连接字符串?

时间:2017-09-29 07:53:05

标签: node.js express express-session

https://github.com/voxpelli/node-connect-pg-simple上的简单示例显示:

var session = require('express-session');

app.use(session({
  store: new (require('connect-pg-simple')(session))(),
  secret: process.env.FOO_COOKIE_SECRET,
  resave: false,
  cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
}));

但是当我尝试它时,节点抱怨:

  

抛出新的错误('没有提供数据库连接的详细信息   连接-PG-简单&#39);

如何指定连接字符串?

2 个答案:

答案 0 :(得分:3)

    // I was stuck with the same proble and solved it

    // 1-) Connection details
    const conObject = {
      user: 'mehmood',
      password: 'mehmood',
      host: 'localhost',// or whatever it may be
      port: 5432,
      database: 'test_db'
    };

    // 2-) Create an instance of connect-pg-simple and pass it session
    const pgSession = require('connect-pg-simple')(session);

    // 3-) Create a config option for store
    const pgStoreConfig = {
      pgPromise: require('pg-promise')({ promiseLib: require('bluebird') })({ 
                         conObject }), // user either this
      //conString: 'postgres://mehmood:mehmood@localhost:5432/test_db', // or this
      // conObject: conObject,// or this,
      // pool: new (require('pg').Pool({ /* pool options here*/}))// or this

    }
   // 4-) use the store configuration to pgSession instance
    app.use(session({
      store: new pgSession(pgStoreConfig),
      secret: 'jW8aor76jpPX', // session secret
      resave: true,
      saveUninitialized: true,
      cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days 
    }));


  // Share improve this answer

答案 1 :(得分:0)

对我来说,它像

conString:'postgres://postgres:password123@localhost:5432/edu';

因此格式必须为

conString:'postgres://<user>:<database_password>@<hostname>:<port>/<database_name';

对于部署,您将获得一个用户名。如果您在本地计算机上运行,​​则PostgreSQL