4

I'm running a Postgres DB and a node app on Heroku. When I try to do

app.use(session({
  store: new pgSession({
    conString: process.env.DATABASE_URL
  }),
  secret: 'my-super-secret-session',
  resave: false,
  cookie: {
    maxAge: 7 * 24 * 60 * 60 * 1000
  }
}));

I get a complaint: error: no pg_hba.conf entry for host "1.2.3.4", user ,myuser", database "mydb", SSL off

I assume I need to tell connect-pg-simple to use SSL somehow?

Shamoon
  • 41,293
  • 91
  • 306
  • 570

2 Answers2

4

If you're not able to edit pg_hba.conf, because you're using a service like heroku, try this.

All you have to do is replace conString with conObject and specify a connectionString and ssl options.

app.use(session({
  store: new pgSession({
    conObject: {
      connectionString: process.env.DATABASE_URL,
      ssl: true,
    },
  }),
  secret: 'my-super-secret-session',
  resave: false,
  cookie: {
    maxAge: 7 * 24 * 60 * 60 * 1000
  }
}));
obedm503
  • 128
  • 1
  • 9
  • 3
    thanks for the solution, however, after applying it, I am now getting `Failed to prune sessions: self signed certificate` . I am using Heroku postgres – SaidAkh Dec 02 '20 at 03:47
1

You need to add an entry in the pg_hba.conf to allow your connection.

Example:

vi $PGDATA/pg_hba.conf
host    all             all             1.2.3.4/32           md5

After saving this config file you will need to reload it by issuing a config reload command:

pg_ctl reload

Then retry the connection.

d1ll1nger
  • 1,571
  • 12
  • 16
  • It's simpler to add the ?ssl=true parameter to the end of the connection string, as explained in these answers: https://stackoverflow.com/questions/22301722/ssl-for-postgresql-connection-nodejs – udik Dec 27 '18 at 14:11