Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing PostgreSQL on Ubuntu for Ruby on Rails

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?

like image 973
Nick Avatar asked Jun 19 '12 00:06

Nick


People also ask

Can I install PostgreSQL on Ubuntu?

PostgreSQL is available in all Ubuntu versions by default. However, Ubuntu "snapshots" a specific version of PostgreSQL that is then supported throughout the lifetime of that Ubuntu version. Other versions of PostgreSQL are available through the PostgreSQL apt repository.


2 Answers

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

like image 85
Nick Avatar answered Oct 05 '22 23:10

Nick


For all Ubuntu 13.10 users that open this thread follow the steps below to install postresql:

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list" wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update sudo apt-get install postgresql-common -t saucy sudo apt-get install postgresql-9.2 libpq-dev 

since there isn't an official Postgres repository for Ubuntu 13.10.

Then create the user as Nick explain (you can specify a password too):

sudo su postgres -c psql postgres=# CREATE ROLE gotqn SUPERUSER LOGIN; postgres=# \password gotqn postgres=# \q 

Note: Replace the gotqn above with whoami result:

enter image description here

The easiest way to create your rails application is to specify you are using postgresql as follows:

rails new Demo -d postgresql 

The code above will automatically add the pg gem in your GemFile and create appropriate database.yml file:

development:   adapter: postgresql   encoding: unicode   database: Demo_development   pool: 5   username: gotqn   password: mypass 

Note: You need to change the username and to specify the correct password if you have set such.

Then run rake db:create and start the rails server.

like image 40
gotqn Avatar answered Oct 06 '22 01:10

gotqn