27

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
2
  • may be problem in database.yml Commented Jul 4, 2016 at 3:00
  • I guess you should specify the port (5432 if you haven't changed postgres defaults) Commented Nov 6, 2019 at 9:03

11 Answers 11

46

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In which file do we have to change this?
The rake task that is not loading the rails environment
In my case (Roda app, not Rails), was similar, changing Rake::TestTask.new(:test) do |t| to Rake::TestTask.new(name: :test, deps: :environment) do |t|. Hope that helps to others.
11

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.

Comments

3

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.

Comments

2

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.

Comments

2

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

Comments

0

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.

Comments

0

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.

Comments

0

I had to restart the server, and make sure you entered username and password. Create development and test databases

Comments

0

If you encounter this error in a rake task, and you use models in an initializer, and you're not using the latest version of Rails, it could be caused by https://github.com/rails/rails/issues/43059 which was fixed by https://github.com/rails/rails/pull/45450.

If you can't upgrade Rails, you may be able work around the issue by delaying the model operation, by using after_initialize:

# config/initializers/my_initializer_which_uses_models.rb
Rails.application.config.after_initialize do
  # your model-using code
end

I encountered this issue in Rails 6.1.7.8. I'm not sure which versions contain the fix (PR-45450)

Comments

-1

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.

Comments

-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.