Intalling the PostgreSQL gem pq for Heroku - ruby-on-rails

First question here, but I want to thank you very much now, because stackoverflow helped me almost 15 times just in the first chapter of michael hartl tut.
Now I'm trying to install PostgreSQL (pg gem) with this command:
`
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end`
but it shows an ERROR
-bash: group: command not found
Then I tried to install directly (?) the gem with
gem install pg
But it shows another ERROR
`Fetching: pg-0.17.1.gem (100%)
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
rvm/rubies/ruby-2.0.0-p353/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
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.
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=/Users/trabalho/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
--with-pg
--without-pg
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/
extconf failed, exit code 1
Gem files will remain installed in .rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/pg-0.17.1 for inspection.
Results logged to .rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/extensions/x86_64-darwin-10/2.0.0/pg-0.17.1/gem_make.out`
I can't understand if it was installed or not, neither how can I confirm. How do I put the group command working? Or how can I install the gem pg and rails_12factor?
Any help?

Are you running ubuntu? Looks like you need this library:
sudo apt-get install libpq-dev
Source

You don't want to run group ... as a command from your terminal but rather put that code block inside of the Gemfile of your rails application.
Gemfile
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Then run the bundle command.
If you are still unable to compile the pg gem this way then you'll need to make sure that you have the xcode command line tools installed (assuming you're using OSX). To install these just run xcode-select --install from your terminal and follow the prompts.

sudo apt-get install libpq-dev
it's helped me on Ubuntu

Related

Can't install pg gem on Rails

I am trying to bundle install, but, for some strange reason, the pg gem is returning the following error on install:
$ gem install pg -v '0.18.4'
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/username/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20151221-23315-1tkv3fd.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
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.
Provided configuration options:
--with-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=/Users/username/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
extconf failed, exit code 1
Gem files will remain installed in /Users/username/.rvm/gems/ruby-2.2.1/gems/pg-0.18.4 for inspection.
Results logged to /Users/username/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-15/2.2.0/pg-0.18.4/gem_make.out
Can anyone please help me install this gem and make my bundle finish successfully?
You should probably install the PostgreSQL development libraries.
If you're on Ubuntu this will help:
sudo apt-get install libpq-dev
On a Mac:
brew install postgresql
You need to configure pg correctly.
Run:
bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/(YOUR POSTGRES VERSION)/bin/pg_config
Then try to run bundle:
bundle install
If the PostgreSQL client library is installed, then you need to tell gem where to look for the include files, which it does by asking pg_config. I use a little shell file to make it easier for gem to find that utility:
#!/bin/sh -x
PATH=/Library/PostgreSQL/9.4/bin:$PATH
gem install pg $#
You could also manually adjust your PATH to always include the PostgreSQL bin directory, which should fix the problem permanently.
If you upgrade your PostgreSQL that path will change, so you might need to reinstall pg and/or change your PATH setting.
Once pg is installed, further upgrades to the gem will be able to install without rerunning the script.

gem install pg not working on Mac El Capitan

I tried to install pg Gem on Mac El Capitan, but I always get this error:
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --with-pg-config=/Users/ricardolopes/Developer/homebrew/bin/pg_config
Using config values from /Users/ricardolopes/Developer/homebrew/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/2.0/usr/bin/ruby
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/
--with-pqlib
--without-pqlib
--with-libpqlib
--without-libpqlib
--with-ms/libpqlib
--without-ms/libpqlib
Gem files will remain installed in /Users/ricardolopes/Code/site-noticias/vendor/bundle/ruby/2.0.0/gems/pg-0.18.3 for inspection.
Results logged to /Users/ricardolopes/Code/site-noticias/vendor/bundle/ruby/2.0.0/gems/pg-0.18.3/ext/gem_make.out
An error occurred while installing pg (0.18.3), and Bundler cannot continue.
Make sure that gem install pg -v '0.18.3' succeeds before bundling.
I also tried:
ARCHFLAGS="-arch x86_64" gem install pg
When I tried it I got:
Building native extensions. This could take a while...
Successfully installed pg-0.18.3
invalid options: -f fivefish
(invalid options are ignored)
Parsing documentation for pg-0.18.3
Done installing documentation for pg after 3 seconds
1 gem installed
But I run bundle install and get the same error posted in the beginning of this post.
I tried reinstall PostgresSQL from brew.
Try this:
brew update
brew install postgresql
sudo gem install pg -v 'VERSION'
This worked for me:
sudo gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
Remember to change 9.4 to your postgres version, if different from 9.4.
For a basic Rails app, you probably already have SQLite installed, but in your Gemfile, it probably has simply:
gem 'sqlite3'
Do you just need PostgreSQL for production? Make sure your Gemfile has it as follows:
group :production do
gem 'pg'
end
Also, make sure your Gemfile has SQLite just for development:
group :development do
gem 'sqlite3'
end
then try running bundle install.

Can't install postgresql gem on mac

I'll be deploying my app on Heroku but wish to develop locally on SQLite. This is the code that I added to my Gemfile.
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'postgresql'
end
When I do bundle install, this is the error that I get.
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
/Users/Fulcrum/.rbenv/versions/2.1.0/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
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.
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=/Users/Fulcrum/.rbenv/versions/2.1.0/bin/ruby
--with-pg
--without-pg
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
extconf failed, exit code 1
Gem files will remain installed in /Users/Fulcrum/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pg-0.17.1 for inspection.
Results logged to /Users/Fulcrum/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/extensions/x86_64-darwin-13/2.1.0-static/pg-0.17.1/gem_make.out
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.
Try bundle install without production:
bundle install --without production
Install the libpq-dev which is missing.
brew install libpq-dev
and then do bundle install, this should solve your problem.

Error with 'pg gem' installation when running 'bundle install'

I'm suddenly running into this issue when running 'bundle install'. Everything seems to be installing correctly, but then I run into an issue with 'pg', as so many others seem to do. As the message says, I try running 'gem install pg - '0.12.2'' but it still fails.
I'm on Snow Leopard 10.6 and this is the error I'm getting:
Installing pg (0.12.2) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/thomaskim/.rvm/rubies/ruby-1.9.3-p327/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
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.
Provided configuration options:
--with-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=/Users/thomaskim/.rvm/rubies/ruby-1.9.3-p327/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
Gem files will remain installed in /Users/thomaskim/.bundler/tmp/1336/gems/pg-0.12.2 for inspection.
Results logged to /Users/thomaskim/.bundler/tmp/1336/gems/pg-0.12.2/ext/gem_make.out
An error occurred while installing pg (0.12.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.12.2'` succeeds before bundling.
First you need to make sure you have command line tools (package for Xcode) installed. Since you're on old version of OSX - you will have to research how to do that.
Than, using homebrew install postgress
brew install postgres
After that, everything should bundle with no issues.
It appears that you do not have postgres installed properly. I got a similar error when I was trying to install a pg module with npm. It disappeared after installing postgres properly.
Here's a link to download: http://www.postgresql.org/download/macosx/
If you run rails on your machine only in dev mode and use sql lite for that, you need don't need to install the production bundles.
So if your gemfile looke like that:
group :production do
gem 'pg'
end
Try installing the bundles with the following command:
gem install --without production

teaching myself rails deploying first app to heroku -failure to upload - sqlite3 and postgre glitch

Im teaching myself rails. Im a programmer but not a web programmer. Im going through Michael Hartl's book here
It mentions that its a good idea to start deploying your app right from the beginning. I agree. So I got an account on Heroku and went through ther setup etc. Then created my 1st app, git and the works. Then followed all the instructions on Heroku's site. Finally I came to pushing my app up to Heroku using this command:
git push heroku master
The book says that there could be problems at this stage because the app on my machine is using sqlite3 while Heroku wants postgresql.
The book suggests changing a line in the gem file of my app from
gem 'sqlite3'
to
gem 'sqlite3-ruby', :group => :development
I tried that and then ran bundle install and then tried to push the app. No luck
I get this message on my console:
An error occured while installing sqlite3 (1.3.6), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.3.6'` succeeds before bundling.
!
! Failed to install gems via Bundler.
!
! Heroku push rejected, failed to compile Ruby/rails app
So I tried what the Heroku website suggests here
to change my gemfile
from this:
gem 'sqlite3'
to this:
gem 'pg'
so I tried this approach and then ran 'bundle install'. But that didn't work either. I get this message on my console:
Installing pg (0.13.2) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/AM/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
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.
Provided configuration options:
--with-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=/Users/AM/.rvm/rubies/ruby-1.9.2-p290/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
Gem files will remain installed in /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2 for inspection.
Results logged to /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2/ext/gem_make.out
An error occured while installing pg (0.13.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.13.2'` succeeds before bundling.
Tried to run this command:
gem install pg
that failed with the following error message:
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/AM/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
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.
Provided configuration options:
--with-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=/Users/AM/.rvm/rubies/ruby-1.9.2-p290/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
Gem files will remain installed in /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2 for inspection.
Results logged to /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2/ext/gem_make.out
Can someone please help me with this. ive hit a roadblock. Not sure what to do next.Thanks
The right configuration in your case has already been posted in other answers and I attach it here for your convenience.
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
However, keep in mind that when you run bundle install, bundler will try to install all gems for all environments. Later, it will load only the gems for the specified environment at runtime.
This is the reason why on your local machine it is trying to install pg as well.
You have two possibilities to solve the compilation error:
Install libpq, the library required by the pg gem to compile. You don't need to install the full PostgreSQL client or stack, libpq is enough to pass compilation. On MacOSX you might want to install it using homebrew.
Tell bundler to skip unnecessary groups when running install.
bundle install --without production
As a side note, please keep in mind that SQLite and PostgreSQL are two different databases. It's always a good idea to use a development environment as similar as possible to the production one. My personal suggestion is to install PostgreSQL on your local machine and use it both on development and production.
Again, homebrew is your friend.
$ brew install postgresql
You'll want to install Postgres locally. It appears that you're on a Mac, so the easiest way would be to use the installer here:
http://www.postgresql.org/download/macosx/
Then stick with this in your Gemfile:
gem 'pg'
So this is what worked:
1) Install postgresql on the machine
2) For permanent fix - Go to the .bash_rc file and add the following:
export PATH = "/Library/PostgreSQL/9.1/bin/":$PATH
OR
just to install the missing files and run the bundle command one time type this in the root of the app:
PATH=$PATH:/Library/PostgreSQL/9.1/bin/ bundle install
or
PATH=$PATH:/Library/PostgreSQL/9.1/bin/ gem install pg
3) Now the pg gem is installed
This post was helpful:
http://excid3.com/blog/installing-postgresql-and-pg-gem-on-mac-osx/
Try
group :production do
gem 'pg'
end
group :development do
gem 'sqlite3'
end
I think the reason for the error that the pg is not installed on your system, so you can not install the gem.
Although heroku not advised to use a different database, I have worked like this:
group :test, :development do
gem 'sqlite3'
end
group :production do
gem 'pg'
end

Resources