1

I've managed to get my Node.js app working properly with my Heroku Postgres database when the node application is deployed to Heroku. However having to deploy to heroku each time I make a code change is a bit arduous and I'd rather be able to develop locally and connect to the database from my local app.

https://devcenter.heroku.com/articles/connecting-to-heroku-postgres-databases-from-outside-of-heroku

The above article doesn't really describe how this is done, I'm not exactly sure what I need to do.

If I attempt to run my app locally and access an endpoint that queries my database I get

Error Error: connect ECONNREFUSED 127.0.0.1:5432

Whereas if I access the same endpoint from my heroku deployed app it works correctly.

Here's my app

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
    }
});

express()
  .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('/users', async (req, res) => {
        try {
            const client = await pool.connect();
            const result = await client.query('SELECT * FROM users');
            const results = { 'results': (result) ? result.rows : null};
            console.log(results);
            res.status(200).json(results);
        } catch (err) {
            console.error(err);
            res.send("Error " + err);
        }
    })
  .listen(PORT, () => console.log(`Listening on ${ PORT }`));

5
  • I think you need to update the the process.env.DATABASE_URL on your local machine. From the error, it looks like the DATABASE_URL refers to localhost while you want to use the db hosted on Heroku. Commented Jul 18, 2020 at 11:12
  • The only database that I have and want to use is the remote one hosted by Heroku. The DATABASE_URL points to that remote db. I don't really want to use a locally hosted db as then I have to maintain two separate db instances and schemas. Essentially I just want to be able to access the remote db from my local app, but I get the ECONNREFUSED error when I do so Commented Jul 18, 2020 at 11:19
  • I understand that you only want to use the remote db only. The error says you're unable to connect to 127.0.0.1:5432. This is the IP address of localhost, not the remote database. Your code is trying to connect to 127.0.0.1:5432 for some reason while it should be trying to connect to the remote db. Commented Jul 18, 2020 at 11:21
  • @YousufKhan I have no idea how I missed that. I just replaced the DATABASE_URL with the actual connection string and now it's working perfectly. Thank you. If you want to post an answer below I'll mark it as correct. Commented Jul 18, 2020 at 11:45
  • ok thanks I am glad I could help Commented Jul 18, 2020 at 11:45

2 Answers 2

1

I think you need to update the the process.env.DATABASE_URL on your local machine. From the error, it looks like the DATABASE_URL refers to localhost while you want to use the db hosted on Heroku.

I understand that you only want to use the remote db only. The error says you're unable to connect to 127.0.0.1:5432. This is the IP address of localhost, not the remote database. Your code is trying to connect to 127.0.0.1:5432 for some reason while it should be trying to connect to the remote db.

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

Comments

0

Did you use the dotenv to access process.env.DATABASE_URL ? You need dotenv.config(); worked for me.

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.