22

Possible Duplicate:
Postgres - the last version 0.14.0 of the “pg” gem gives error

I'm new to rails. I want to use PostgreSQL since Heroku uses it too, but I'm having a bad time installing the pg gem. Apparently, some files are missing and a Makefile cannot be created?

My gem file looks like this:

source 'https://rubygems.org'

gem 'rails', '3.2.6'
gem 'faker', '1.0.1'
gem 'pg', '0.12.2'
gem 'json'

group :development, :test do
  gem 'rspec-rails', '2.10.0'
end

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails', '2.0.0'

group :test do
  gem 'capybara', '1.1.2'
  gem 'factory_girl_rails', '1.4.0'
end

And when I run bundle install I get the following error when installing the pg gem:

    Installing pg (0.14.0) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb 
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)
*** 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.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
    --with-pg
    --without-pg
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-pg_config
    --with-pqlib
    --without-pqlib
    --with-libpqlib
    --without-libpqlib
    --with-ms/libpqlib
    --without-ms/libpqlib


Gem files will remain installed in /Users/KennyGWang/.bundler/tmp/18833/gems/pg-0.14.0 for inspection.
Results logged to /Users/KennyGWang/.bundler/tmp/18833/gems/pg-0.14.0/ext/gem_make.out
An error occured while installing pg (0.14.0), and Bundler cannot continue.
Make sure that `gem install pg -v '0.14.0'` succeeds before bundling.

I installed postgresql using homebrew, brew install postgresql, thus which psql gives me /usr/local/bin/psql

Any help is appreciated!

Community
  • 1
  • 1
knygw
  • 382
  • 2
  • 3
  • 14

4 Answers4

81

Are you on a Mac? If so - the easiest way to do this is to

  1. make sure you have Xcode installed (you can get it from the app store)
  2. use Homebrew
  3. brew install postgresql
  4. gem uninstall pg
  5. either bundle install in your application's root or gem install pg
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
plasticide
  • 1,242
  • 9
  • 13
  • 1
    Just a note on #1. It's very important to have Xcode installed and working properly in order for the postgresql formula to work. The compiler is used to build the PostgreSQL source. – plasticide Jul 12 '12 at 20:22
  • Worked for me on Mac OSX Mountain Lion (10.8.3). – user456584 May 06 '13 at 22:00
  • working on Mavericks OSX 10.9.2 thanks! – d1jhoni1b Apr 02 '14 at 21:26
  • I tried this but didn't seem to work for me. This ended up being the solution that fixed it for me http://stackoverflow.com/a/26609483/1438478. – Fralcon Nov 14 '14 at 07:21
  • 2
    Just to add to this as I came across it searching Google, I had a similar problem on OSX Yosemite: the above didn't work for me but this did: http://stackoverflow.com/a/7497725/1945990 – user115014 Dec 18 '14 at 15:36
  • i just want to say that i am totally amazed this actually worked because usually postgres is a pile and takes forever to fix – botbot Apr 26 '17 at 04:22
  • On Ubuntu: -> sudo apt-get install postgresql-client libpq5 libpq-dev -> sudo gem install pg – Gaurav Patil Mar 18 '21 at 09:07
10

Steps to install postgresql

  1. Install PostgreSQL and its libraries

    sudo apt-get install postgresql postgresql-contrib libpq-dev
    
  2. After the installation create a user for postgresql

    sudo -u postgres createuser --superuser $USER
    

    or

    sudo -u postgres createuser pgs_root
    
  3. Set user password for the postgresql user

    sudo -u postgres psql postgres --set "user=$USER"
    postgres=# \passsword :user
    

    or

    sudo -u postgres psql postgres
    postgres=# \passsword pgs_root
    
  4. 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 'localhost'

    gedit /etc/postgresql/8.4/main/postgresql.conf listen_addresses = 'localhost'
    

Like mysql query browser, there is a GUI for postgresql called pgadmin. To install run this command

sudo apt-get install pgadmin3
Aymane
  • 13
  • 1
  • 3
Addicted
  • 749
  • 1
  • 6
  • 19
4

Try to include this instead of pg

gem install postgres-pr

or

gem install ruby-pg

Edit

Did you do this already?

$ sudo apt-get install postgresql

$ rails new pg -d postgres

http://www.funonrails.com/2011/03/getting-started-with-rails-3-postgres.html

Sully
  • 14,672
  • 5
  • 54
  • 79
  • "sudo gem install ruby-pg" didn't work, but the first did. I'm still missing this: "checking for PQconnectdb() in -lpq... no" i need the pg gem to run "rake db:migrate" ! – knygw Jul 11 '12 at 18:53
0

have you checked the log mkmf.log as it was recommended in the error you've included?

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.

bento
  • 2,079
  • 1
  • 16
  • 13