Installing PostgreSQL on Ubuntu for Ruby on Rails

Nick picture Nick · Jun 19, 2012 · Viewed 38.2k times · Source

I currently have Ruby on Rails installed via RVM in Ubuntu 12.04. The default database is set up in SQLite3, but I'd like to switch to PostgreSQL for the purposes of pushing to Heroku. How can I accomplish this?

Answer

Nick picture Nick · Jun 19, 2012

Here are the steps I've followed:

Install PostgreSQL and development package

$ sudo apt-get install postgresql
$ sudo apt-get install libpq-dev

Set up a user that is the same as my Ubuntu log-in

$ sudo su postgres -c psql
postgres=# CREATE ROLE <username> SUPERUSER LOGIN;
postgres=# \q

Modify Gemfile

# Remove gem 'sqlite3'
gem 'pg'

Modify database.yml in app directory

development:
  adapter: postgresql
  encoding: unicode
  database: appname_development
  pool: 5
  timeout: 5000
  username: <username>
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: appname_test
  pool: 5
  timeout: 5000
  username: <username>
  password:

Run bundle install

$ bundle install

Create databases and migrations

$ rake db:create:all
$ rake db:migrate

Here are the sources I used to help:
http://mrfrosti.com/2011/11/postgresql-for-ruby-on-rails-on-ubuntu/
http://railscasts.com/episodes/342-migrating-to-postgresql
https://devcenter.heroku.com/articles/local-postgresql