Elliot Blackburn
Elliot Blackburn

Reputation: 4164

Fresh rails app defaulting to Postgres instead of SQLite3

I've just installed rbenv, ruby 2.2.3 and rails 4.2.4 for the first time on this machine. I've started my rails application with no change to any of the code, just the default generated documents from using rails new ., I then started the server with rails server.

When hitting http://localhost:3000 I'm getting the following error:

"Specified 'postgresql' for database adapter, but the gem is not loaded. Add gem 'pg' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."

I've got postgres installed from a previous project with Node, but my database.yml still reads as you'd expect from a new application:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

I don't really want to use Postgres at the moment, I'm just starting out and I'd rather keep things simple with SQLite3 for a bit. Does anyone know what may be going on and what I could do to get it using SQLite3 so that this error stops?

Upvotes: 3

Views: 561

Answers (3)

Bart C
Bart C

Reputation: 1547

It's an old post but maybe somebody will find this useful.

In my case I had the DATABASE_URL variable exported in ~/.bash_profile like so

# Postgres installation
export DATABASE_URL=postgres:///$(whoami)

On Linux it might be in ~/.bashrc file.

Just remove or comment out the export line if you don't use Postgres anymore or use AlienBishop's solution if you do.

As IliaAptsiauri mentioned, without DATABASE_URL variable applications should use settings from database.yml file.

Upvotes: 2

Rob Johansen
Rob Johansen

Reputation: 5164

There is another solution, for those who actually need to keep the DATABASE_URL environment variable without affecting Rails (like me). You can use a url sub key:

development:
  <<: *default
  url: sqlite3:db/development.sqlite3

This is documented here.

Upvotes: 4

mr. Holiday
mr. Holiday

Reputation: 1800

The problem is that when you start the server it is looking for environment variable DATABASE_URL which is probably set to postgres and this takes precedence over the database.yml file. You can delete the environment variable, and it should work, or you can reset it to SQLite.

Upvotes: 2

Related Questions