How To Setup Ruby on Rails with Postgres

Postgres is a great open source database management system. It's pretty easy to use postgres with Ruby on Rails.

Step 1: Install postgress in your system. If you already have postgres installed in your system then skip it else follow the link.

Step 2: Create new project
rails new pg_demo
You can also create using

rails new pg_demo --database=postgresql

Step 3: include the pg gem in your gem file

gem 'pg'

Step 4: run bundle command
bundle

Step 5: It will give you some error like
An error occurred while installing pg (0.17.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.17.1'` succeeds before bundling.

So let's try to install the  pg gem first
gem install pg

It will again throw an error  :(

Building native extensions.  This could take a while...
ERROR:  Error installing pg:
ERROR: Failed to build gem native extension.

    /home/sabyasachi/.rvm/rubies/ruby-2.1.0/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

To solve the problem just run the next line
sudo apt-get install libpq-dev

After this, install pg gem :-

gem install pg
And then
bundle

Step 5: Go the the database.yml file and change the development config

development:
  adapter: postgresql
  encoding: unicode
  database: pg_demo_dev
  host: localhost
  pool: 5
  username: postgres
  password: password.

That's it.... :) We are done . 

Comments

  1. Great information shared. We are Ruby on Rails web Development company who offer best price in the industry and complete value of money to clients.

    ReplyDelete

Post a Comment

Popular posts from this blog

Insert Bulk data using active record import

Swap primary keys between two records in the same table in Postgres

How to add a bootstrap table with fixed header and scrollable body