Murakami
Murakami

Reputation: 3770

Connection pool error in Rails application with Postgres

I am having an issue when my Rails app is deployed in AWS ECS. Many of requests finish with 200 but there are some (every let's say 50) finish with 500. The log says:

ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.)

I changed my pool (in database.yml config) to 15, also set RAILS_MAX_THREADS to 15, and indeed it happens rarely now but still the problem occurs.

What am I missing here?

Ruby version: 2.6.6 Postgres: 11.5

database.yml:

production:
  adapter: postgresql
  encoding: utf8
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOSTNAME'] %>
  port: <%= ENV['DB_PORT'] %>
  pool: 25

Upvotes: 1

Views: 462

Answers (1)

Raj Verma
Raj Verma

Reputation: 1172

Try and use this have common values in default and then depending upon env have individual values for the other environments, which can be based on environment variables:

default: &default
  adapter:  postgresql
  encoding: unicode
  port:     <%= ENV.fetch("POSTGRESQL_PORT", "5432") %>
  pool:     <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['POSTGRESQL_USER_NAME'] %>
  password: <%= ENV.fetch("POSTGRESQL_PASSWORD", "somepassword") %>
  host:     <%= ENV['POSTGRESQL_HOST'] %>

development:
  <<: *default
  database: <%= ENV['POSTGRESQL_DB'] %>-development
  host: db

production:
  <<: *default
  database: <%= ENV['POSTGRESQL_DB'] %>

Upvotes: 1

Related Questions