Getting PostgreSQL set up on Ubuntu bash running on Windows - ruby-on-rails

I recently installed and set-up rails on an Ubuntu bash running on Windows as a Linux subsystem. I was able to get rails up and going without a problem and create a new rails application on my Windows C:/ drive. I tried downloading PSQL directly from the postgresql.org/downloads/windows website but had some issues so I canceled, then followed the Installing PostgresQL section of this tutorial. The only issue is that the previous download which was canceled was set to listen to the default Port 5432, and it appears to have kept the port occupied because the second installation said it had to use Port 5433.
EDIT: It appears that the original PSQL had been successfully installed as it appeared in my program list, I was able to uninstall it but did get an error warning that the data folder was not removed. After uninstalling and having only one installed copy of PSQL, I still get the same error when running the below sudo service postgresql start.
EDIT EDIT: I uninstalled both versions of PSQL completely and re-installed the version in the linked tutorial and it got rid of the second error I was having. However, my 'pg' gem is still not bundle installing.
Once that was complete, I tried switching over my existing rails application to postgresql. I updated the database.yml file and added the 'pg' gem to my Gemfile. I cannot, however, bundle install. When I try, I get this error:
Fetching pg 1.0.0
Installing pg 1.0.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /home/<user_profile>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.0.0/ext
/home/<user_profile>/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20180717-4727-vex5p2.rb 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.
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=/home/<user_profile>/.rbenv/versions/2.5.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
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/home/<user_profile>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/pg-1.0.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /home/<user_profile>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.0.0 for
inspection.
Results logged to
/home/<user_profile>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/pg-1.0.0/gem_make.out
An error occurred while installing pg (1.0.0), and Bundler cannot continue.
Make sure that `gem install pg -v '1.0.0' --source 'https://rubygems.org/'` succeeds before bundling.
In Gemfile:
pg
EDIT: Below is solved, refer to second edit above.
Similarly, when I try to start postgresql from the command line using sudo service postgresql start, I get the following:
$ sudo service postgresql start
* Starting PostgreSQL 9.5 database server * The PostgreSQL server failed to start. Please check the log output:
2018-07-17 14:59:26 DST [4858-1] LOG: could not bind IPv4 socket: Permission denied
2018-07-17 14:59:26 DST [4858-2] HINT: Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
2018-07-17 14:59:26 DST [4858-3] WARNING: could not create listen socket for "localhost"
2018-07-17 14:59:26 DST [4858-4] FATAL: could not create any TCP/IP sockets
Does anybody know what is going wrong with the installation of PostgresQL in my environment and how I can get it successfully running on my Ubuntu shell?

Found the solution to these problems.
Issue #1 - 'pg' gem not installing
Execute the following in bash:
sudo apt-get install libpq-dev
gem install pg
bundle install
Issue #2 - postgresql cannot start without crashing
Uninstall all versions on PSQL on computer and reinstall one. The failed installation and the second version were sharing their data folder and caused errors with the installation I was using.

Related

AWS CodeBuild process for Rails fails to install pg gem

I am trying to deploy Rails to Elastic Beanstalk using a simple 3-step CodePipeline process. Step 1 takes the source from GitHub, gives it to CodeBuild, which then should create the build and pass it to EB.
For some reason, when the CodeBuild process reaches the pg gem, it fails with the following error:
Fetching pg 1.3.5
Installing pg 1.3.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory:
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/pg-1.3.5/ext
/root/.rbenv/versions/2.7.2/bin/ruby -I
/root/.rbenv/versions/2.7.2/lib/ruby/2.7.0 -r ./siteconf20220419-56-10cqcfm.rb
extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... yes
Using config values from /usr/bin/pg_config
Using libpq from /usr/lib64
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... yes
checking for PQconninfo() in libpq-fe.h... no
Your PostgreSQL is too old. Either install an older version of this gem or
upgrade your database to at least PostgreSQL-9.3.
*** 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=/root/.rbenv/versions/2.7.2/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-gvl-unlock
--disable-gvl-unlock
--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
--with-pq-dir
--without-pq-dir
--with-pq-include
--without-pq-include=${pq-dir}/include
--with-pq-lib
--without-pq-lib=${pq-dir}/lib
--with-pqlib
--without-pqlib
To see why this extension failed to compile, please check the mkmf.log which can
be found here:
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.3.5/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/pg-1.3.5 for inspection.
Results logged to
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.3.5/gem_make.out
An error occurred while installing pg (1.3.5), and Bundler cannot continue.
Make sure that `gem install pg -v '1.3.5' --source 'https://rubygems.org/'`
succeeds before bundling.
In Gemfile:
pg
[Container] 2022/04/19 14:56:37 Command did not exit successfully bundle install exit status 5
[Container] 2022/04/19 14:56:37 Phase complete: INSTALL State: FAILED
[Container] 2022/04/19 14:56:37 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: bundle install. Reason: exit status 5
I have the following barebones buildspec.yml:
version: 0.2
phases:
install:
commands:
- bundle install
artifacts:
files:
- '**/*'
The bundle process runs without error on my local machine.
I had the same problem you had above too (except without using CodeBuild). The issue is that you're trying to install the pg gem at version 1.3.5.
At the time of writing, Elastic Beanstalk on Amazon Linux 2 (AL2) ships with Postgres version 9.2 by default (incredibly old ๐Ÿ˜”). Whereas the pg gem dropped support for 9.2 after version 1.2.3. See here.
So your 2 options are either:
Enhance EB to install a more recent version of Postgres during the build & deploy process, for example as described here: https://stackoverflow.com/a/63204453/1852005.
Or you could just use version 1.2.3 of the pg gem by configuring that version in your Gemfile. That version shipped in 2020-03-18.
Either of those options should get you past that error.
Note that your application may also connect to Amazon's RDS which has a more recent Postgres version. However that RDS lives on another server, so don't get the two confused! You can see the difference if you eb ssh to the server and run e.g: this on the shell directly:
[root#ip-XXX-XX-XX-XXX current]# psql --version
psql (PostgreSQL) 9.2.24
compared with running this on a Rails console which shows the RDS version:
irb(main):001:0> ActiveRecord::Base.connection.select_value('SELECT version()')
=> "PostgreSQL 11.13 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-12), 64-bit"

Fail to install pg gem in Rails (using PostgreSQL 9.6)

Hi could someone please help me! Appreciate it a lot!
I have some problem bundle install because the terminal says I fail to install gem pg v 0.20.0
When I tried to install the gem pg this error occurs. I'm currently using PostgreSQL 9.6 I have tried through all the solutions in the similar question but nothing works, including trying to reconfigure pg.
198-195:Review minhvu$ gem install pg -- --with-pg-config=/usr/pgsql-9.6/bin/pg_config
Building native extensions with: '--with-pg-config=/usr/pgsql-9.6/bin/pg_config'
This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
current directory: /Users/minhvu/.rvm/gems/ruby-2.3.0/gems/pg-0.20.0/ext
/Users/minhvu/.rvm/rubies/ruby-2.3.0/bin/ruby -r ./siteconf20170313-7016-1if947v.rb extconf.rb --with-pg-config=/usr/pgsql-9.6/bin/pg_config
Using config values from /usr/pgsql-9.6/bin/pg_config
sh: /usr/pgsql-9.6/bin/pg_config: No such file or directory
sh: /usr/pgsql-9.6/bin/pg_config: No such file or directory
* 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/minhvu/.rvm/rubies/ruby-2.3.0/bin/$(RUBY_BASE_NAME)
--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}/lib
/Users/minhvu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/mkmf.rb:456:in try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/minhvu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/mkmf.rb:541:intry_link0'
from /Users/minhvu/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/mkmf.rb:556:in try_link'
from extconf.rb:40:in'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/minhvu/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-16/2.3.0/pg-0.20.0/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Users/minhvu/.rvm/gems/ruby-2.3.0/gems/pg-0.20.0 for inspection.
Results logged to /Users/minhvu/.rvm/gems/ruby-2.3.0/extensions/x86_64-darwin-16/2.3.0/pg-0.20.0/gem_make.out
It seems to me, that you are trying to installing pg gem, but first you should install PostgreSQL in your system. Depending of what system you have you should follow the instructions in this post. I am quoting the answer from addicted Addicted user at the following post
Rails Error Installing PG
Steps to install
Install PostgreSQL and its libraries
sudo apt-get install postgresql postgresql-contrib libpq-dev
After the installation create a user for postgresql
sudo -u postgres createuser --superuser $USER
or
sudo -u postgres createuser pgs_root
Set user password for the postgresql user
sudo -u postgres psql postgres ( For psql prompt) postgres=# \passsword for ex.- postgres=# \passsword pgs_root
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

Failure to build native extension

I am new to rails and attempting to switch my default DB to Postgres from SQLite3. I have encountered an error. I am using gem install pg and getting this error error: Error installing pg: ERROR: Failed to build gem native extension. I am using Mavericks OSX and Ruby 1.9.3, Rails 3.2.12, and PG 9.3 โ€“The goal is to deploy a sample app to heroku (which doesnt seem to support SQLite.
Steps I have tried:
link 1: Installing PG gem on OS X - failure to build native extension
link 2(heroku instructions): https://devcenter.heroku.com/articles/sqlite3
EDITED TO INCLUDE SPECIFIC ERROR MESSAGE BELOW:
HEAD https://rubygems.org/latest_specs.4.8.gz
302 Moved Temporarily
HEAD https://s3.amazonaws.com/production.s3.rubygems.org/latest_specs.4.8.gz
304 Not Modified
HEAD https://rubygems.org/specs.4.8.gz
302 Moved Temporarily
HEAD https://s3.amazonaws.com/production.s3.rubygems.org/specs.4.8.gz
304 Not Modified
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/.gemtest
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/BSDL
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ChangeLog
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/Contributors.rdoc
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/History.rdoc
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/LICENSE
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/Manifest.txt
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/POSTGRES
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/README-OS_X.rdoc
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/README-Windows.rdoc
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/README.ja.rdoc
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/README.rdoc
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/Rakefile
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/Rakefile.cross
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/errorcodes.def
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/errorcodes.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/errorcodes.txt
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/extconf.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/gvl_wrappers.c
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/gvl_wrappers.h
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg.c
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg.h
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg_connection.c
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg_errors.c
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/pg_result.c
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/vc/pg.sln
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/vc/pg_18/pg.vcproj
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/ext/vc/pg_19/pg_19.vcproj
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg/connection.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg/constants.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg/exceptions.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/lib/pg/result.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/array_insert.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/async_api.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/async_copyto.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/async_mixed.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/check_conn.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/copyfrom.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/copyto.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/cursor.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/disk_usage_report.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/issue-119.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/losample.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/minimal-testcase.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/notify_wait.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/pg_statistics.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/replication_monitor.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/test_binary_values.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/wal_shipper.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/sample/warehouse_partitions.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/data/expected_trace.out
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/data/random_binary_data
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/lib/helpers.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/pg/connection_spec.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/pg/result_spec.rb
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/pg-0.17.0/spec/pg_spec.rb
Building native extensions. This could take a while...
/usr/local/rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
*** 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=/usr/local/rvm/rubies/ruby-1.9.3-p392/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
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:461:in `try_link0'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:476:in `try_link'
from extconf.rb:39:in `<main>'
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
Building has failed. See above output for more information on the failure.
Gem files will remain installed in /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/pg-0.17.0 for inspection.
Results logged to /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/pg-0.17.0/ext/gem_make.out
It's trying to build the gem pg, which has C or C++ code in it, and the compilation phase is not working. Under OS X, you must install XCode or get a compiler by other means, and the shell you're using to run bundle install must have the compiler in its path. I've compiled pg many times under OS X, though I have not upgraded to Mavericks.
If you have XCode installed, the above paragraph doesn't apply. You'll get additional information on what's going wrong if you try gem install pg. Try that and post the results.
The key is "You have to install development tools first." This is a strong hint that you don't have a proper compiler. This page says XCode must be v5.01 or later.
Thanks all for your help. The specific issue was resolved by me running the below command in terminal
brew install apple-gcc42
To clarify, I had the most recent XCode (with command line) and Mavericks, Ruby 1.9.3, Rails 3.2.12, and PG 9.3. I am unsure why that specific command worked when I already had the command line tools installed. The one change of the output of
gcc--version
was that --prefix=/Library/Developer/CommandLineTools/usr
changes to
--prefix=/Applications/Xcode.app/Contents/Developer/usr
Again, this change worked in practice, but I am not sure why this fixed things.

Cannot install pg gem in Mavericks with Postgres.app

I am trying to install the pg gem for use with Postgres.app on my local machine. I am running Mavericks.
Postgres.app is installed and running fine, but I cannot get the gem to work. I've done the following:
Used the command 'env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config' from the Postgres.app documentation
Updated Homebrew and installed the Apple GCC 4.2
Installed the Xcode developer tools
Updated my $PATH to reference both the Postgres.app bin and lib directories
All with no success. Here is the specific error message I receive:
Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config'
This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/Brian/.rvm/rubies/ruby-2.0.0-p353/bin/ruby extconf.rb --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Using config values from /Applications/Postgres.app/Contents/MacOS/bin/pg_config
sh: /Applications/Postgres.app/Contents/MacOS/bin/pg_config: No such file or directory
sh: /Applications/Postgres.app/Contents/MacOS/bin/pg_config: No such file or directory
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/Brian/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
--with-pg
--without-pg
--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}/
I'd appreciate any help you can offer. Thanks!
You probably have the wrong path for --with-pg-config, check if it's actually there.
You can find the correct path to pg_config with:
find /Applications -name pg_config
In the latest Postgres.app Version the path is:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config
In my case (running Postgres.app v9.3.4.2) it only seemed to work when prepending the environment architecture flags:
env ARCHFLAGS="-arch x86_64" gem install pg -- \
--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
Being on a brand new mac, here's what I had to do:
Install Xcode tools from the app store
Open Xcode tools and accept the license
Now run (hopefully a future-proofed command):
version=$(ls /Applications/Postgres.app/Contents/Versions/ | tail -1)
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/$version/bin/pg_config .
If you have trouble, you can troubleshoot a bit by checking out the actual error in the mkmf.log which you can find by running (if using rvm):
cd ~/.rvm ; find . -name mkmf.log | grep pg
I was able to install pg with this command
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
I found my path by running
sudo find / -name "pg_config"
and I Successfully installed pg-0.17.1
adding the postgress bin dir to the path also does the trick. just add the bin to the pat like this. with recent installs the latest symbolic link makes sure that this path should be 'stable' for future version upgrades.
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
To resolve this issue I installed postgres using homebrew using the following in the terminal window:
brew install postgres
Homebrew can be found here
http://brew.sh
This worked for me:
gem install pg -- --with-pg-config=`which pg_config`
For future reference, since a lot of us are posting new paths for the new version numbers:
Currently I'm seeing a symlink called latest in /Applications/Postgres.app/Contents/Versions/.
You should be able to just do:
$ gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config
And forget about the version number. This might not apply to every (older) version, but I myself was looking for a snippet I could save and reuse.

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

Resources