SECURITY WARNING: Never run commands you don't understand. Always review code before execution. Use at your own risk.
Ruby New Added 9 October 2026

Rails: could not obtain a connection from the pool within 5.000 seconds

Each thread that touches the database needs its own connection, and every connection in the pool stayed checked out for longer than checkout_timeout. The cause is rarely a slow database: it is usually a pool smaller than the number of threads, such as Puma threads or Sidekiq concurrency set higher than pool in database.yml. Threads you start yourself that never return their connection drain it too.

Quick fix

Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.

Quick fix
# Pool size against the threads that use it
bin/rails runner 'p ActiveRecord::Base.connection_pool.stat'
grep -n pool config/database.yml
grep -n threads config/puma.rb

# config/database.yml: at least one connection per thread in each process
production:
  pool: <%= ENV.fetch("RAILS_MAX_THREADS", 5) %>

# Sidekiq: concurrency must not exceed the pool
RAILS_MAX_THREADS=10 bundle exec sidekiq -c 10

# Your own threads must hand the connection back
Thread.new do
  ActiveRecord::Base.connection_pool.with_connection { Report.generate }
end

How to diagnose Ruby errors

Ruby errors are dominated by Bundler and gem resolution, native extension compilation (which needs system headers Ruby cannot install for you), and in Rails by the pending-migration and asset-precompilation guards. Those guards are deliberate safety checks, so the fix is to satisfy them rather than to disable them.

If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.

  1. Run bundle install with verbose output and read which gem fails and why. Native extension failures name the missing header in the mkmf log.
  2. Check the mkmf log path the error prints. It contains the actual compiler error, which the Bundler summary truncates.
  3. For Rails migration errors, run rails db:migrate:status to see exactly which migrations the database has recorded.
  4. For asset errors in production, confirm rails assets:precompile ran during deployment and that the manifest is present in public/assets.
  5. Verify the Ruby version matches .ruby-version and the Gemfile. Version managers silently switch, producing gem-not-found errors for gems that are installed elsewhere.

Tools worth reaching for

  • bundle install --verbose
  • rails db:migrate:status
  • gem env
  • rbenv/rvm version check
  • bundle exec

Authoritative references

Primary documentation for this error, worth reading before applying any fix in production.

api.rubyonrails.org guides.rubyonrails.org

Related Ruby errors

See all 20 Ruby errors →

Browse other categories

Something missing or wrong?

This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.