0

I am writing an Express JS API and trying to host it on Heroku. I have all the endpoints set up and locally when I run the command "heroku local web" it is working fine. I am trying to connect to heroku production database when the NODE_ENV is set to production and when it's not it'll connect to my local database, but the code below seems to not be working. I am able to connect to the heroku database if I hardcode the database credentials to my .env file, but I would like to be able to go back and forth from my local databse to heroku production database depending on NODE_ENV

const Pool = require('pg').Pool
const isProduction = process.env.NODE_ENV === 'production'
const connectionString = `postgresql://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_DATABASE}`
const pool = new Pool({
  connectionString: isProduction ? process.env.DATABASE_URL : connectionString,
  ssl: isProduction
})

any help will be very appreciated.

  • You're using the wrong quotes. `${}` only works inside `\`backquotes\``. – user229044 Jul 29 '20 at 00:54
  • @meagar that's not the problem because heroku continues to try to connect to my local database url instead of the database on heroku. I am trying to connect to heroku database using DATABASE_URL ***** – abc123dropthemic Jul 29 '20 at 01:01

0 Answers0