Wednesday, February 29, 2012

Installing Postgresql & using it in rails 3 application

If your are going to build a portal application which is going to hold large data then trust me "Postgresql" is the best option.

1. Install PostgreSql

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

2. After the installation is complete, create a user (like we create root user in mysql)

sudo -u postgres createuser pgs_root

This will create a user named pgs_root.

3. Now add password to the user

sudo -u postgres psql postgres
running this command will take you to psql prompt

postgres=# \passsword <user>

whatever password you wish to add must be entered twice, then type \q to exit psql prompt.

3. Configure the postgresql.conf file to make PostgreSQL listen to localhost or listen on an external IP or something, change this line to either the IP or '*' or 'localhost'.

/etc/postgresql/8.4/main/postgresql.conf (here 8.4 is the postgresql version, change it if you have installed different version)

under "CONNECTIONS AND AUTHENTICATION" uncomment the following line

listen_addresses = 'localhost'

check whether you have the following line in your /etc/postgresql/8.4/main/pg_hba.conf


# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD


# "local" is for Unix domain socket connections only
local   all         all                               ident
# IPv4 local connections:
host    all         all         127.0.0.1/32          md5

5. Now postgreSQL setup is complete. To use in the application add the "pg" gem in Gemfile

gem "pg"

run bundle install

6.  In order for your application to connect with database present in PostgreSQL, configure it in the database.yml

   development:
    adapter: postgresql
    database: test_postgres
    username: pgs_root
    password: *******
    host: 127.0.0.1

7.  Create the database

    rake db:create

This will create a database named test_postgres in PostgreSQL and it is ready to use.

There is a nice GUI tool for Postgresql named pgadmin3. To install:

sudo apt-get update
sudo apt-get install pgadmin3

All the best...!!!


No comments:

Post a Comment