Edgar Latorre

I'm Edgar and this is my blog where I write about software development.

Rails and PostgreSQL

13 May 2013

Considering that you have rails installed.

First step install postgres:

sudo apt-get install postgresql-9.2 postgresql-server-dev-9.2

Starting a new rails application:

rails new myapp -d postgresql

Access the postgres console:

sudo -su postgres psql

create a role on postgres:

create role myapp with createdb login password 'secret';

Configure the database on your application: config/database.yml

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

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

If you have some problem to access the database, edit the pg_hba.conf: /etc/postgresql/9.2/main/pg_hba.conf

change the line:

local   all             all                                     peer

to:

local   all             all                                     trust

Restart postgreSQL:

sudo /etc/init.d/postgresql restart

Now we can use Ruby on Rails and Postgres.