banditKing
banditKing

Reputation: 9579

rails app defaults to sqlite3. I would like to have postgresql as default _ how to change

I am learning rails and when I started and installed everything I went with sqlite3 based on the books that I was reading. However, I was trying to deploy a test app that I wrote on heroku. Heroku recemmends that the apps run postgresql for development and production to prevent subtle bugs. So I decided to henceforth use post gresql for all my rails apps.

To do this I did the following set up:
1) Downloaded and installed Postgresql
2) added the path to the folder in my .bash_rc file
3) installed the pg gem
4) Everything so far was good. However, when I go to the root folder of my app and try this command:

rails generate scaffold User name:string email:string

I get the following error on the console

rails generate scaffold User name:string email:string

invoke  active_record 
/Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/bundler-
1.0.22/lib/bundler/rubygems_integration.rb:143:in `block in replace_gem': 
Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` 
(sqlite3   is not part of the bundle. Add it to Gemfile.) (LoadError)
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/activerecord- 
3.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:3:in `<top (required)>'

This tells me that the default setting in my rails app is for sqlite3 clearly. So I opened my databse.yml file and this si what it had.

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# 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:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Everything was set up for sqlite3 How can I change my rails settings such that by default all the configs for database are for postgresql?

Thanks

Upvotes: 2

Views: 2349

Answers (2)

nmarley
nmarley

Reputation: 41

Not sure if this is a new feature since you asked your question, but pass '-d postgresql' to the rails new command and it will populate the config/database.yml with postgres info.

rails new testapp -d postgresql

Upvotes: 3

Peter Brown
Peter Brown

Reputation: 51707

Update: Check out - http://railscasts.com/episodes/342-migrating-to-postgresql

You just need to change the settings in your database.yml file so it knows what you want to use. In your case you'll want to change the adapter and database:

development:
  adapter: postgresql
  database: my_app_dev
  username: username
  password: pw

Once you've got that in there, you can run the following to get it to match what you had in sqlite:

rake db:create && rake db:schema:load

Upvotes: 6

Related Questions