简体   繁体   中英

Rails: No connection pool for ActiveRecord::Base

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

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.

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.

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.

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.

我不得不简单地重新启动服务器以使警告消失。

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM