【问题标题】:How to specify the connection string in connect-pg-simple?如何在 connect-pg-simple 中指定连接字符串?
【发布时间】:2020-08-14 22:32:55
【问题描述】:

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
}));

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

throw new Error('未提供数据库连接详细信息 连接-pg-simple');

如何指定连接字符串?

【问题讨论】:

    标签: node.js express express-session


    【解决方案1】:
        // 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
    

    【讨论】:

      【解决方案2】:

      对我来说它就像

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

      所以格式一定是

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

      为了部署,你会得到一个用户名。如果你在本地机器上运行,那么 PostgreSQL

      【讨论】:

        猜你喜欢
        • 2017-04-19
        • 2014-07-25
        • 2011-07-30
        • 2016-12-30
        • 2014-06-11
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 2016-10-22
        相关资源
        最近更新 更多