PostgreSQL Server Not Responding to Node.js Request – How to Fix

node.jspostgresql

I have access to a remote postgres DB from pgAdmin4 and I also could access from nodejs using a Mac. Right now I'm using the same code to access the DB in Windows. The code for my connection is the following:

const { Client } = require('pg'); //Importing the Postgres package
const hosts= require('../hosts'); //Using the file containig all hosts 
const connectionData = { //Begin creating the connection settings object
   host: hosts.DBHost, //DB host   
   port: hosts.DBPort, //DB hosts port
   database: hosts.DB, //DB
   user: hosts.DBUser, //DB user
   password: hosts.DBPassword, //DB user password
 } 

My test is the following:

var client = new Client(connectionData); //New client instance using the above connection settings
client.connect(); //Open the connection to the database()  
sql = "select * from myTable";
client.query(sql) 
  .then(response => {
    console.log ({"data": response}); //This isn't shown 
  })
  .catch(err => { 
    console.log({"error": err}); //This isn't shown neither 
  })

No error, no exception, the DB server doesn't respond!

Why isn't the server responding?

Best Answer

The problem is that I have Nodejs 12 in my Mac, but I have NodeJS 14 in my Windows, so it's a compatibility problem.

Since I updated my postgres (pg) to 8.0.3 or superior (i.e. npm install pg), now it works!

One guy helped me to realise that issue in https://stackoverflow.com/questions/61611039/postgres-server-is-not-responding-to-a-nodejs-request?noredirect=1#comment108987882_61611039.