Skip to content
Advertisement

Express.js app times out when attempting to query a PostgreSQL database

I’ve built an Express.js app, hosted by Heroku, with a non-embedded PostgreSQL database.

The app was running as intended until about a month ago, and for several months before that. However, when I logged in today, I ran into a serious problem.

Whenever I try to access any page which queries the database, it tries to load for a few seconds, and then gives me the standard Heroku “Application Error” page.

Accessing the Database through Javascript

This is what a portion of my code that queries the database looks like:

var that = this;
var title = "The Journal";
var queryString = "SELECT * FROM JournalEntry "+
                  "ORDER BY theTimeStamp ASC;";
var extract, data;
const client = new Client({
    connectionString: process.env.DATABASE_URL,
    ssl: { rejectUnauthorized: false }
});

console.log(process.env.DATABASE_URL);
console.log("Hello!");

client.connect();

console.log("My!");

client.query(queryString, (err, result) => {
    if(err) throw err;

    console.log("Lovely!");

    extract = result.rows;
    data = interpretJournalExtract(extract);
    data = objectifyExtract(data);
    client.end();
    that.finaliser.protoRender(req, res, "tabular",
                               { title: title, data: data });
});

And this is what the logs look like when I run this code:

2021-03-02T16:12:14.780345+00:00 app[web.1]: postgres://stfonefosxlmgt:227d3914dfb105c6ad3db47ad854f771391b15c8dead2da0a691a66d25cb2f65@ec2-54-228-237-40.eu-west-1.compute.amazonaws.com:5432/de337svimlvbgo
2021-03-02T16:12:14.780701+00:00 app[web.1]: Hello!
2021-03-02T16:12:14.781269+00:00 app[web.1]: My!
2021-03-02T16:12:44.784363+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/journal" host=hgmj.herokuapp.com request_id=f28bdda2-f3bd-40b2-a894-e07cb9e0054c fwd="86.191.159.122" dyno=web.1 connect=1ms service=30012ms status=503 bytes=0 protocol=http
2021-03-02T16:12:44.785541+00:00 app[web.1]: GET /journal - - ms - -

You’ll notice that the logs get to “Hello!” and “My!”, but not to “Lovely!”.

Accessing the Database through the Heroku CLI

It’s also worth noting that I can access the database through the Heroku CLI, and this is what my screen looks like when I do so:

-> Connecting to postgresql-curly-19296
psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1), server 11.11 (Ubuntu 11.11-1.pgdg16.04+1))
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

hgmj::DATABASE=>

The Bottom Line

Why is this happening, and what can I do about it?

Advertisement

Answer

I had same problem. adding

ssl: { rejectUnauthorized: false }

parameter to connection worked. I needed to upgrade pg package for it to work.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement