No connection pool for ActiveRecord::Base

社会主义新天地 提交于 2019-12-18 12:18:52

问题


I'm trying to use rails 4.2.6 to develop an app. I'm trying to use postgres for database. Server starts fine but when I try loading a page it throws this "No connection pool for ActiveRecord::Base" error.

What could it be?

EDIT

The pg gem wasn't working properly. I had to comment it before starting the server and then uncomment it from my GemFile afterwards. I realized that I was using Ruby 2.3 instead of Ruby 2.0 (as intended). I removed ruby 2.3 and set up everything under ruby 2.0 environment. It's now working properly.

I had read somewhere that there were some issues with the 'pg' gem in newer Rails releases, requiring people to use 'gem install pg --pre' instead for installing the gem. I tried that, but then my app was requiring the 'pg' gem in my GemFile and, well, the problem stated above showed up again.

This is how my database.yml file ended up:

  default: &default
     adapter: postgresql
     encoding: unicode
     host: localhost
     username: -------
     password: -------
     pool: 5

  development:
     <<: *default
     database: myDbName

回答1:


If you are experiencing this error from a rake task, chances are you are not running the :environment task before your task.

Changing:

task :task_name do
end

to:

task task_name: :environment do
end

Should fix the issue.




回答2:


This issue arises when the server cannot find the corresponding database it depends on to pull data from.

I had same issue from my end, I updated my version of Sqlite3, and it was higher than the version that the current Puma Server version supports.

I simply had to uninstall the Sqlite3 version, and then install the version supported by the current version of Puma Server.

gem uninstall sqlite3

This will uninstall the updated version of Sqlite3, and then you run the code below stating the version supported by your current server.

gem install sqlite3

Or you can as well open your Gemfile, and then include the version for the database that you are using

gem 'sqlite3', '~> 1.3.6' 

N/B: The sqlite3 version is the most current version as at the time of writing this answer

And then run

bundle update

to install the version of the database that you specified.

That's all.

I hope this helps.




回答3:


Check the database.yml if all settings are ok. If its the first time and you didn't create the database yet use this command to create the database

rake db:create

If the database already exists try reset the db migrations,

rake db:migrate:reset

Hope it'll solve the problem. Go to rails console and try something to check if its working or not.




回答4:


For PostgreSQL your database.yml file should look something like that:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default
  database: your_db_name

Also make sure that you have the gem installed: in your Gemfile:

gem 'pg'

Finally, restart your server.

Hope that helps

Edit: I almost forgot, make sure you have your PostgresSQL running, check this link for download and setup.




回答5:


I've encountered the same problem when I try to access the Model before creating the DataBase.

Make sure you run rake db:migrate at least once.

If you already run migration then check the Database as mentioned in previous answers.




回答6:


Rails 5

New app

Using Postgresql for both test and development

Specs run, so Rails can connect to Postgresql

But when I started the web app, I got "No connection pool with id primary found."

Ran 'bin/rails db:migrate:reset' and restarted the app. It worked. No idea why.

database.yml:

    development:
      adapter: postgresql
      encoding: unicode
      database: rails5_development
      pool: 5
      username: foo
      password: bar
      host: localhost
      port: 5432

    test:
      adapter: postgresql
      encoding: unicode
      database: rails5_test
      pool: 5
      username: foo
      password: bar
      host: localhost
      port: 5432



回答7:


I had to simply restart the server for the warning to go away.




回答8:


when you are running rails db:migrate in data base rows will be created according to your migration files. you can see schema for further info.




回答9:


In my case, the config/database.yml was taking variables from the environment:

# ...
test:
  <<: *default
  database: <%= ENV.fetch("DB_NAME") %>_test
  username: <%= ENV.fetch("DB_USER") %>
  password: <%= ENV.fetch("DB_PASS") %>
# ...

The error was coming when the new terminal window where I was running the bundle exec rails spec did not have those variables initialized.

Doing,

$ export DB_NAME=mydb
$ export DB_USER=myuser
$ export DB_PASS=mypass
$ bundle exec rails spec

fixed the issue.



来源:https://stackoverflow.com/questions/38176304/no-connection-pool-for-activerecordbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!