ECONNREFUSED When Attempting GET on PSQL Database Using Node.js and node-postgres

E-Commerce App Client

I receive the following error when using my browser to make a GET request for all entries in a database table:

Error: connect ECONNREFUSED
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
Error in GET /products

The relevant folder structure looks like:


The files involved are:

const express = require("express");
const cors = require("cors");
const app = express();

const PORT = process.env.port || 3001;


// const usersRouter = require('./routes/users')
// app.use('/users', usersRouter)
// const authRouter = require('./routes/auth')
// app.use(['/login', '/register'], authRouter)
const productsRouter = require('./routes/products')
app.use('/products', productsRouter)
// const cartsRouter = require('./routes/carts')
// app.use('/carts', cartsRouter)
// const ordersRouter = require('./routes/orders')
// app.use('/orders', ordersRouter)

app.listen(PORT, () => {
  console.log(`Listening on ${PORT}`);

Initializes the server on port 3001 and creates productsRouter.


const { Pool, Client } = require("pg");

const pool = new Pool({
  user: "postgres",
  host: "localhost",
  database: "ecommerce",
  password: "postgres",
  port: "5432",

module.exports = {
  query: (text, params, callback) => {
    return pool.query(text, params, callback)

Provides the function for making queries.


const express = require("express");
const products = express.Router();
const db = require("../db/index");

products.get("/", async (req, res) => {
  try {
    const products = await db.query("SELECT * FROM products");
    return res.status(201).send(products.rows);
  } catch (err) {
    console.log("Error in GET /products");
    res.status(500).send("Server Error Occurred");

Provides Node.js logic for making requests using db.query calls.

Steps to produce error:

  1. Launch/connect Postbird on port 5432 on localhost.
  2. Run ‘node index.js’ on server/index.js to start Node.js server listening on port 3001.
  3. Navigate browser to localhost:3001/products to initiate GET request.

Can someone please explain how I can resolve this?

Hey there,

Can you confirm that Postgres is actually running? Can you run netstat and see if that port is currently occupied?

If you aren’t familiar with netstat and finding an application using the port, please refer to the following post on StackOverflow

So it turns out that when Codecademy told me to install Windows Subsystem for Linux (WSL), I didn’t realize it was truly, essentially a mostly separate system from my main OS. I was using WSL’s terminal in VS Code and when I ran ‘node index.js’, index.js was running on WSL. Therefore, it was unaware of the perfectly good, perfectly accessible instance of Postgresql running on port 5432 of my main OS.

Basically, it was refusing to connect to port 5432 on WSL because there wasn’t any Postgres instance listening on that port of WSL; in fact, it wasn’t even installed on WSL.

I switched to my Bash terminal, ran ‘node index.js’, navigated my browser to the URL that calls the GET request which returns the results of a SELECT on all tables in my database, and voila! It worked.

So in conclusion, I now vehemently hate WSL because I don’t understand its purpose since the Bash terminal running inside VS Code on my Windows computer works perfectly fine.

1 Like

That makes sense. For future reference, it looks like there is a way to access the Postgres SQL instance running in WSL from Windows.

1 Like