Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Rails 3.1 app with Postgresql database (OSX Lion)

I cant seem to find an up-to-date guide to creating a new Rails 3.1 app with a Postgresql database. I would greatly appreciate a guide through that process.

like image 667
neon Avatar asked Oct 12 '11 14:10

neon


2 Answers

Since Rails 3, the framework is completely database-agnostic.

What that means is, all you have to do is 2 things:

  1. Include the pg gem in your Gemfile: gem 'pg'
  2. Make sure your database.yml file is using postgresql as the adapter.

You can accomplish this automatically when you create a new app by appending the --database=postgresql flag:

rails new myapp --database=postgresql

But if you've already created the app, then just comment out gem 'sqlite3' in your Gemfile, add in gem 'pg', run bundle, and then change your database.yml file to use the correct adapter.

Of course, it goes without saying that you will also need to install PostgreSQL itself, but that is something you can easily find on google.

like image 140
bricker Avatar answered Nov 09 '22 09:11

bricker


To elaborate on bricker's answer... After running:

$ rails new myapp --database=postgresql

Here is what your database.yml will look like:

development:
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  username: myapp
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: myapp_test
  pool: 5
  username: myapp
  password:

production:
  adapter: postgresql
  encoding: unicode
  database: myapp_production
  pool: 5
  username: myapp
  password:

Running:

$ bundle exec rake db:create:all

will create the databases for you if you have PostgreSQL installed. The easiest way to install PostgreSQL is http://postgresapp.com/

like image 40
Lance Fisher Avatar answered Nov 09 '22 08:11

Lance Fisher