Noam
Noam

Reputation: 5341

Can't connect to heroku postgres from heroku local using node sample

I followed the steps at: https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

When I ran heroku local it couldn't connect - I saw that it was using the process.env.DATABASE_URL and got it to my local .env file using: https://devcenter.heroku.com/articles/heroku-local

But it still wouldn't connect, I've added a console.log to see the error:

"error: no pg_hba.conf entry for host "62.90.xxx.yyy", user "username", database "password", SSL off"

What now?

Upvotes: 1

Views: 702

Answers (2)

Dmitry Verhoturov
Dmitry Verhoturov

Reputation: 6082

A neat solution would be enabling SSL only for the Heroku server connection. For that add dialectOptions.ssl=true to Heroku environment in config.json file:

  {
"production": {
    "use_env_variable": "DATABASE_URL",
    "dialectOptions": { "ssl": true },
    "logging": false
  }

Upvotes: 0

Noam
Noam

Reputation: 5341

After a lot of searches it turns out that added 'pg.defaults.ssl = true' solves the problem for me while allowing me to stay as close as possible to the sample provided by Heroku.

Here is my code

const cool = require('cool-ascii-faces');
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000

const pg = require('pg');
pg.defaults.ssl = true; //this is it!!!

express()
  .use(express.static(path.join(__dirname, 'public')))
  .set('views', path.join(__dirname, 'views'))
....

Upvotes: 1

Related Questions