João
João

Reputation: 761

Error trying to run seeds: Knex: Timeout acquiring a connection. The pool is probably full

I'm getting this error everytime i run yarn knex seed:run:

Error while executing "/home/user/path-to-the-file/my-seed.js" seed: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?

The problem is that i send the project for other people and they can run it normally, i already tried all answers about it of the internet, i don't know what to do anymore.

My database config:

const config = Object.freeze({
  client: 'pg',
  pool: {
    min: 0,
    max: 5,
    idleTimeoutMillis: 30000,
    createTimeoutMillis: 3000,
    acquireTimeoutMillis: 30000,
    reapIntervalMillis: 1000,
    createRetryIntervalMillis: 100,
    propagateCreateError: false
  },
  connection: Object.freeze({
    ...database
  })
})

I already tried to downgrade pg and node, it didnt work, nothing works.

There is a issue on Knex repository saying that upgrading to latest pg solves it, but it didnt work too: https://github.com/knex/knex/issues/2820

Can someone help me?

Upvotes: 3

Views: 3434

Answers (2)

krishnazden
krishnazden

Reputation: 1177

faced similar issued, bumped pg to 8.7.1 and it worked

also node to 16.3.1 and knex to 0.95.11

Upvotes: 0

Mikael Lepistö
Mikael Lepistö

Reputation: 19718

One easy way to create this error is doing this:

const rows = [];
for (let i =  0; i < 10000; i++) {
  rows.push({ name : `foo  ${i}` });
}
await Promise.all(rows.map(data => knex('user').insert(data)));

There are infinite ways how to run out of connections, but most usual cases are opening too many concurrent transactions or running huge amount of parallel queries.

Best way to find out the reason why it happen is to remove parts of the problematic code until the error doesn't occur anymore and then investigate why that reduced test case fails.

One good way to see what knex is doing internally is to set DEBUG=knex:* environment variable, before running the code so that knex outputs information about queries, transactions and pool connections while code executes.

Upvotes: 1

Related Questions