I'm trying to make request to a postgreSQL DB being accessed by a Node + Express server that I've deployed on Heroku. This is the error im getting when running 'heroku logs': Error: connect ECONNREFUSED 127.0.0.1:5432, along with a couple other errors. I'm using axios on the frontend to make request.
Here is my server.js file:
// express
const express = require('express');
// environment variables
require('dotenv').config();
// data base connector
const db = require('./db');
// cors
const cors = require('cors');
// express instance
const app = express();
// use cors
app.use(cors());
// attach data to req.body
app.use(express.json());
// get all articles
app.get('/api/v1/articles', async (req, res, next) => {
try {
// query database
const results = await db.query('SELECT * FROM articles;');
// send results
res.status(200).json({
results: results.rows
});
// handle error
} catch (err) {
next(err);
}
});
// get individual article
app.get('/api/v1/articles/:id', async (req, res, next) => {
try {
// query database
const article = await db.query(`SELECT * FROM articles WHERE id = $1;`, [req.params.id]);
// send back data
res.status(200).json({
article: article
});
// handle error
} catch (err) {
next(err);
}
});
// get article's comments
app.get('/api/v1/articles/:id/comments', async (req, res, next) => {
try {
// query database
const comments = await db.query(`SELECT * FROM comments WHERE article_id = $1;`, [req.params.id]);
// send back data
res.status(200).json({
comments: comments
});
// handle error
} catch (err) {
next(err);
}
});
// add comment
app.post('/api/v1/articles/:id/comments', async (req, res, next) => {
try {
// post comment in database
const comment = await db.query(`INSERT INTO comments (article_id, author, text)
VALUES ($1, $2, $3) returning *;`, [req.body.article_id, req.body.author, req.body.text]);
// send comment back
res.status(201).json({
comment
});
// handle error
} catch (err) {
next(err);
}
})
// listen
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log(`Server now listening on PORT ${port}`);
});
I'm supposedly connected to my DB with the pg library, and the Pool gets the credentials from an .env file. I'm able to connect to the DB within pgAdmin and write queries, make tables etc. Any help greatly appreciated
Check your database connection configuration. The error shows that it tries to connect to PostgreSQL on 127.0.0.1
which is LOOPBACK ip address. This means that is trying to access the server on your machine. You need to configure the connection by specifying the correct address for the server where PostgreSQL is running