2

I'm trying to connect to a PostgreSQL database using Knex.js, but I just can't get a connection to happen. The only exception I'm seeing is:

Error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?

I built this simple test script to make sure it wasn't part of our program:

const knex = require("knex")({
    client: 'pg',
    connection: {
        host : 'localhost',
        port: 5432,
        database: 'postgres',
        user: 'postgres',
        password: 'password'
    },
    pool: {
        afterCreate: (conn, done) => {
            console.log("Pool created");
            done(false, conn);
        }
    },
    debug: true,
    acquireConnectionTimeout: 2000
});

console.log("A")

const a = knex.raw('select 1+1 as result').then(result => console.log("A Success", result)).catch(err => console.log("A Error", err));

console.log("B")

const b = knex.select("thing").from("testdata").then(data => console.log("B Success", data)).catch(err => console.log("B Error", err));

console.log("C")

const c = knex.transaction(trx => {
    trx.select("thing").from("testdata")
        .then(data => {
            console.log("C Success", data);
        })
        .catch(err => {
            console.log("C Error", err);
        });
})
    .catch(err => {
        console.log("C Error", err);
    });

console.log("waiting on query")
// Promise.all([a, b, c]).then(() => {
//     console.log("Destroying")
//     knex.destroy()
// })

Which is producing the following output

A
B
C
waiting on query
A Error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
    at Client_PG.acquireConnection (E:\DEV\work\niba-backend\node_modules\knex\lib\client.js:347:26)
    at runNextTicks (internal/process/task_queues.js:58:5)
    at listOnTimeout (internal/timers.js:520:9)
    at processTimers (internal/timers.js:494:7) {
  sql: 'select 1+1 as result',
  bindings: undefined
}
B Error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
    at Client_PG.acquireConnection (E:\DEV\work\niba-backend\node_modules\knex\lib\client.js:347:26) {
  sql: undefined,
  bindings: undefined
}
C Error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
    at Client_PG.acquireConnection (E:\DEV\work\niba-backend\node_modules\knex\lib\client.js:347:26)
    at async Transaction.acquireConnection (E:\DEV\work\niba-backend\node_modules\knex\lib\transaction.js:213:28)

It's never calling the afterCreate method. I have tried it against both our dev database using settings that work for everyone else, and a locally running postgres installation with every combination of settings I could come up with. I even passed this off to another member of the team and it worked fine, so there's something up in my machine, but I have no idea what could be blocking it. I'm not seeing any connection attempts in the postgres logs, and I can't seem to get any better error messages to work off of.

If anyone could come up with things I can try, or ways to get more information out of Knex I would really appreciate it.

1
  • You could try removing afterCreate completely and make sure that your local machine postgres is running. Also in your transaction handler you should return a Promise from the handler to trigger implicit commit / rollback. Basically everything seems to point to a direction that postgres has been configured in a way that it is not accepting TCP connections. Commented Oct 20, 2020 at 7:52

1 Answer 1

6

I traced the issue down to the verion of the 'pg' package we were using. Was using 7.18 and when I upgraded to the latest version (8.4) it started connecting. No idea why the 7.x version wasn't working.

Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU SO MUCH FOR POSTING THIS. :) We had code that stopped working and changing the version of pg worked. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.