0

I'm using express-session and trying to use Postgres as my session store:

app.use(session({
    store: new (require('connect-pg-simple')(session))(),
    secret: 'mysessionsecret',
    resave: false,
    cookie: {
      maxAge: 7 * 24 * 60 * 60 * 1000
    }
  }));

But when I run my server, I get

2016-05-07T14:19:05.571491+00:00 app[web.1]: error: no pg_hba.conf entry for host "my.ip.address", user "myuser", database "databasename", SSL off

What am I doing wrong?

Shamoon
  • 41,293
  • 91
  • 306
  • 570

2 Answers2

2

Try this:

var pgSession = require('connect-pg-simple')(session);
app.use(session({
    store: new pgSession({
        conString : process.env.DATABASE_URL
     }),
     secret: 'mysessionsecret',
     resave: false,
     cookie: {
         maxAge: 7 * 24 * 60 * 60 * 1000
     },
     secure : true
 }));
Yoni Rabinovitch
  • 5,171
  • 1
  • 23
  • 34
1

There is no entry in PostgreSQL's host-based authentication file pg_hba.conf for host "my.ip.address", user "myuser", database "databasename", with SSL off.

You probably have PostgreSQL configured to only accept connections from the loopback address 127.0.0.1.

See the manual for pg_hba.conf and more generally the client authentication chapter.


OK, so connect-pg-simple gets its connection info from the DATABASE_URL environment variable by default. From the comments you're trying to connect to a Herkoku PostgreSQL instance, presumably from outside Heroku where DATABASE_URL isn't set. So you should set the DATABASE_URL env var to an appropriate value, making sure sslmode=require is included (see the Heroku documentation) or pass the URL as the connString parameter to connect-pg-simple.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Well, that would've been a useful detail to include at the start. For Heroku your problem is that *Heroku requires SSL connections* and *you are not attempting to connect with SSL*. You seem to be using some kind of wrapper called `connect-pg-simple`; [the manual for it](https://www.npmjs.com/package/connect-pg-simple) suggests that it reads the connection string from `DATABASE_URL` in the environment if running on Heroku etc. If not available, you should specify a connection string. I don't know Node.js, but I suggest passing the parameter `connString` as your Heroku `DATABASE_URL`. – Craig Ringer May 09 '16 at 13:45
  • Right - but I'm not specifying the DB connection anywhere, so I'm not sure where to specify the connection for the session – Shamoon May 09 '16 at 13:48
  • @Shamoon See the manual for `connect-pg-simple` linked above; set `DATABASE_URL` in the environment or pass the `connString` parameter. – Craig Ringer May 09 '16 at 13:50