Question about Environment/ Config Vars- Heroku and Postgres

Ive managed to slowly grind my way through the curriculum and just completed the backend portion.

I decided to look into building some practice project using Heroku…

If this is not the place to post his please let me know.

While following the tutorial here Getting Started on Heroku with Node.js | Heroku Dev Center, I get to the portion where we connect to the Postgres server (‘Provision a database’). I do this successfully but I get side tracked on figuring out how to access/use this Postgres database that is located and provided through Heroku, on my local machine. Please consider the following two methods:

Method #1

const cool = require("cool-ascii-faces");
const express = require("express");
const path = require("path");
const PORT = process.env.PORT || 5000;
const { Pool } = require("pg");

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  ssl: {
    rejectUnauthorized: false,

  .use(express.static(path.join(__dirname, "public")))
  .set("views", path.join(__dirname, "views"))
  .set("view engine", "ejs")
  .get("/", (req, res) => res.render("pages/index"))
  .get("/db", async (req, res) => {
    try {
      const client = await pool.connect();
      const result = await client.query("SELECT * FROM test_table");
      const results = { results: result ? result.rows : null };
      res.render("pages/db", results);
    } catch (err) {
      res.send("Error " + err);
  .get("/cool", (req, res) => res.send(cool()))
  .get("/times", (req, res) => res.send(showTimes()))
  .listen(PORT, () => console.log(`Listening on ${PORT}`));
const showTimes = () => {
  let result = "";
  const times = process.env.TIMES || 5;
  for (i = 0; i < times; i++) {
    result += i + " ";
  return result;

I run this code and get this error–>

Method #2
Exactly the same code from Method #1 except I take the following steps prior to running:

Kill and reopen terminal at file location (/node-js-getting-started)> Using the credentials information (User, Password, Host, Port, and Database) in the heroku datastore resources tab I run the following in order:


heroku local web

Open browser and profit.

My question is, why does the one connect and the other doesn’t?

It took me days of research/digging to resolve (ended up just sort of winging it so Im hoping it’s actually working how I imagine it should), and I’m afraid that it took so long to resolve because I’m asking or searching the wrong questions. So if you guys have any resources on this topic I would appreciate the nudge in direction.