Friday, May 11, 2012

Rails + PostgreSQL on Mac Lion

1) Install PostgreSQL

$brew install postgresql

Once the installation is complete it is important to follow all the instructions that brew created for us!
Here is a list of instructions to initialize a database named postgres and run postgresql on start-up:

$ initdb /usr/local/var/postgres
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist

or follow the instruction generated after installing.

2) Create PostgreSQL users

Login to psql console:

$ psql -h localhost postgres

If all is good we should see the command prompt:


Now lets create a user postgres:

postgres=# CREATE USER postgres WITH PASSWORD 'postgres';

And give it all permissions for postgres database that we created earlier:

postgres=# GRANT ALL PRIVILEGES ON DATABASE postgres to postgres;

3) Install pg gem

env ARCHFLAGS="-arch x86_64" gem install pg

4) Configure Rails project

Add the following lines to database,yml file:

    adapter: postgresql
    database: postgres
    username: postgres
    password: postgres
    host: localhost
    encoding: UTF8

Add pg gem to Gemfile:

gem 'pg', :require => 'pg'

No comments:

Post a Comment