Postgree too many connections in rails console
我正在使用postgre gem开发一个Ruby on Rails应用程序,这就是我的database.yml的样子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | development: adapter: postgresql encoding: utf-8 pool: 5 username:"hytxlzju" password:"xxxxx" host:"jumbo.db.elephantsql.com" port:"5432" database:"hytxlzju" production: adapter: postgresql encoding: utf-8 pool: 5 username:"hytxlzju" password:"xxxxxx" host:"jumbo.db.elephantsql.com" port:"5432" database:"hytxlzju" |
每当我在本地连接到这个数据库时,从
[编辑]
这是错误消息:
1 2 | C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/postgresql_adapter.rb:12 22:in `initialize': FATAL: too many connections for role"hytxlzju" (PG::ConnectionBad) |
[编辑]我添加了我的启动器,仍然没有成功:
1 2 3 4 5 6 7 8 9 10 | Rails.application.config.after_initialize do ActiveRecord::Base.connection_pool.disconnect! ActiveSupport.on_load(:active_record) do config = ActiveRecord::Base.configurations[Rails.env] || Rails.application.config.database_configuration[Rails.env] config['pool'] = ENV['DB_POOL'] || ENV['RAILS_MAX_THREADS'] || 5 ActiveRecord::Base.establish_connection(config) end end |
您可以尝试以下方法
Active Record limits the total number of connections per application
through a database settingpool ; this is the maximum size of the
connections your app can have to the database
在
1 | pool: <%= ENV['RAILS_MAX_THREADS'] || 5 %> |
如果您使用的是puma,请在此处使用
它可能会解决问题。
[解决了]
不知怎的,我的演示应用程序没有找到表中的条目,所以它创建了多个池连接,而没有关闭它们,因为在关闭连接之前,会抛出500错误,因此我关闭池的那段代码从来没有 关闭。 更多邻近postgre会话
https://devcenter.heroku.com/articles/concurrency-and-database-connections#connection-pool