1

Heroku site states that the DATABASE_URL is setup automatically for you. I used the command heroku config to confirm that the DATABASE_URL is indeed set.

However when I use the pg package command

const client = new Client({
    connectionString: process.env.DATABASE_URL,
    ssl: true,
});

and do a console.log(process.env.DATABASE_URL), the variable reads as undefined.

The other errors that I am getting are:

UnhandledPromiseRejectionWarning: Error: The server does not support SSL connections

The complete code is:

const express = require('express');
require('dotenv').config();

const { Client } = require('pg');
const app = express();

console.log(process.env.DATABASE_URL);

const client = new Client({
    connectionString: process.env.DATABASE_URL,
    ssl: true,
});

 client.connect();

 client.query('SELECT * FROM customers;', (err, res) => {
    if (err) throw err;
    for (let row of res.rows) {
        console.log(JSON.stringify(row));
    }
    client.end();
});


app.get('/', (req, res) => { 
    res.send('Hello World')
});


app.listen(4000, () => {
    console.log(`Server started on port`);
});

The code works when I use my local postgresql database, but when I try to connect to Heroku's postgres database, the above errors occur. Any suggestions?

4
  • Where you're getting this error? In your local setup or heroku console? Commented May 6, 2020 at 15:38
  • The error is in the local setup. Commented May 6, 2020 at 18:09
  • I am doing exactly the same thing and getting the same error :( Did you figure out what the problem is? Commented May 7, 2020 at 5:17
  • @legend12 The DATABASE_URL will be defined only in the heroku production environment. You'll obviously get this error if you try locally. Commented May 7, 2020 at 6:22

2 Answers 2

1

Seems you're not crazy... This isn't working for me either, so I dug in, and it just seems... broken.

https://stackoverflow.com/a/19341505/4526479

I see DATABASE_URL defined in the heroku config vars section in the online heroku dashboard. But it's just undefined in the app.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you're running into issues connecting to the Heroku Postgres database when you run the project locally.

The DATABASE_URL environment variable specified in heroku config exists only on the Heroku server and you don't have the environment variable set locally.

Create a .env file and include your connection string like so

DATABASE_URL=...

Here you can include the connection string for the database hosted on Heroku, or your local Postgres database server. Just make sure SSL is configured correctly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.