Using : Ruby 1.9.2, Rails 3.1.3
Please note : I’m new to all this Ruby on Rails stuff, I’m posting these articles so if I make mistakes hopefully people will point them out.
As I found out today while getting back into Ruby on Rails (for the third attempt) Heroku now uses PostgreSQL as it’s database data store. It therefore makes sense to use this database type as your local development db rather than sqlite3 so you don’t come across any issues later upon deployment.
As with all rails stuff these instructions will depend on the versions you are running with, so please take note of the date this article was published. This is also the reason I wanted to write up how I got it to work, as it seems a number of pages that show up in Google searches are a bit out of date.
1, First step is to install PostgreSQL. You can download and install it from here:
I installed it with all the defaults
2, Next install the PostgresSQL drivers for Ruby by using Ruby Gems. At a terminal prompt type
gem install pg
3, You can now go ahead and create a new rails application, but this time you want to add an extra argument to let rails know you want to use PostgreSQL:
rails new myApplication -d postgresql
4, Now you need to edit your database.yml file to let rails know how to connect to your PostgreSQL instance. This is stored in the config directory of your rails application. There will be 3 different database settings for development, test and production. Update each sections to include the username and password, here’s how the development connection will look like:
5, Save database.yml and go back to your terminal prompt. Now lets generate a table, model, view and controller for some product objects:
rails g scaffold products name:string description:text stock_level:integer
6, Once this has run you can use the rake command to first create the databases and then run the migration scripts
7, If you open up your pgAdmin application which PostgreSQL should have installed in Launchpad you’ll be able to see your 3 new databases. If you expand myApplication_development through the Schemas and public nodes you will be able to see that 2 tables have been created. The products table and a table for keeping track of migrations.
8, The final test to see if it is all working is to type:
and browse to http://localhost:3000/products – and try using the application!