bwad
bwad

Reputation: 81

local Postgres connection not returning anything

I’m trying to perform a simple query on a local database. I expect this query to return the schema names of the database.

enter image description here

I am running Postgres version 13.1 and I installed it by following the steps shown here: https://postgresapp.com/

enter image description here

As per guidelines on Postgres Wiki, I'm including config file changes, I only manually edited settings to enable logging.

enter image description here

This computer is running MacOS Big Sur Version 11.0.1. I'm using Node.js and Postgres is running on port 5432 and I can access it with psql.

The relevant changes I've made are the following:

enter image description here

Endpoint in server.js:

router.post('/mock_call', async (ctx) => {
   try {
     console.log('sup')
     await sql.mockCall()
 
     ctx.body = {
       status: "It's good",
       data: 'good'
     }
   } catch (e) {
     console.log(e)
     ctx.body = {
       status: "Failed",
       data: e
     }
     ctx.res.statusCode = 422;
   }
 })

SQL File:

require("openssl")
 
const { Pool, Client } = require('pg');
 
const client = new Client({
   user: 'user1',
   host: 'localhost',
   database: 'postgres',
   password: 'mypass',
   port: 5432,
});

module.exports = {

mockCall: function () {
 
   console.log('mockCall begin')
   client.connect(err => {
     if (err) {
       console.error('connection error', err.stack)
     } else {
       console.log('connected')
     }
   })
   console.log('before query')
   client.query("SELECT schema_name FROM information_schema.schemata", (err, res) => {
     if (err) {
       console.log('theres an error:')
       console.log(err)
     };
     console.log('theres a response:')
     console.log(res)
     for (let row of res.rows) {
       console.log(JSON.stringify(row));
     }
     client.end();
   });
 }
}

Logs that actually get printed out when I hit the endpoint on localhost:

sup
mockCall begin
before query

Postgresql Logs (not helpful it's as if the server never gets hit): enter image description here

This exact project and code is working on my personal local computer and the query goes through as expected. It used to be working on a Heroku server I had set up. The only difference with the Heroku server is that the connection is made like so:

const client = new Client({
 connectionString: process.env.DATABASE_URL,
 ssl: {
   rejectUnauthorized: false
 }
});

This connection had been working on a server I had for over a year. My database was running out of space so I upgraded from a hobby database to a standard plan on Heroku, the app continued to work. A couple weeks after this upgrade I pushed a new commit which included a couple new features on the app and this broke the postgresql connection. After this push I immediately checked out my last commit which was working and pushed that one, the issue however was still there.

I currently have the program running on my personal local computer but I need to move it back to Heroku as quickly as possible. The pictures and logs I've included above are the result of running my app locally on my friends computer, which seems to be having the same issue I'm having on Heroku so I'm hoping if I figure out the issue on his local computer I'll be able to solve what's going on in Heroku.

These are the logs that are printed out from my personal local computer which is working:

enter image description here

Edits:
Running psql -d postgres -U user1 -h localhost -p 5432 successfully connects me to the database on the command line.

The new features I added was a new endpoint for my apps customers. This commit works fine on my personal local computer, so I don't think it's an issue with the new features that I added. Additionally, since then I've reverted to my previous commit which used to be working so none of that new code is present anymore.

I'm running the entire app locally on my friends computer. I set up Postgres from scratch just as I did a year ago on my computer. However now, only my personal local computer is working.

I haven't changed anything on pg_hba.conf on either setup. This is what they both look like:

enter image description here

At first I thought the problem would be with Heroku since my local app was working fine. However after reaching out and talking for a couple days with support they said:

Hi there,

It looks like your application is able to successfully connect to the database, but something else in the application or framework is preventing the data from being retrieved. Unfortunately, as this is an application issue it falls outside the nature of the Heroku Support policy. I recommend searching our Knowledge Base or asking the community on Stack Overflow for more answers.

Upvotes: 1

Views: 400

Answers (1)

bwad
bwad

Reputation: 81

Turns out I was using an old version of pg, 7.8. I upgraded to 8.5 and now it works.

Upvotes: 1

Related Questions