969

I am using the Ruby on Rails 3.1 pre version. I like to use PostgreSQL, but the problem is installing the pg gem. It gives me the following error:

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

        /home/u/.rvm/rubies/ruby-1.9.2-p0/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=/home/u/.rvm/rubies/ruby-1.9.2-p0/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 /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
    Results logged to /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out

How do I solve this problem?

Cadoiz
  • 1,446
  • 21
  • 31
demonchand
  • 11,091
  • 6
  • 21
  • 25

49 Answers49

1534

It looks like in Ubuntu that header is part of the libpq-dev package (at least in the following Ubuntu versions: 11.04 (Natty Narwhal), 10.04 (Lucid Lynx), 11.10 (Oneiric Ocelot), 12.04 (Precise Pangolin), 14.04 (Trusty Tahr) and 18.04 (Bionic Beaver)):

...
/usr/include/postgresql/libpq-fe.h
...

So try installing libpq-dev or its equivalent for your OS:

  • For Ubuntu/Debian systems: sudo apt-get install libpq-dev
  • On Red Hat Linux (RHEL) systems: yum install postgresql-devel
  • For Mac Homebrew: brew install postgresql
  • For Mac MacPorts PostgreSQL: gem install pg -- --with-pg-config=/opt/local/lib/postgresql[version number]/bin/pg_config
  • For OpenSuse: zypper in postgresql-devel
  • For ArchLinux: pacman -S postgresql-libs
Julien
  • 953
  • 9
  • 15
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • 1
    didn't work for me on ubuntu 11.04 `durrantm@castleLinux2011:~/Dropbox_not_syncd/webs/3/linker$ gem install libpq-dev ERROR: Could not find a valid gem 'libpq-dev' (>= 0) in any repository durrantm@castleLinux2011:~/Dropbox_not_syncd/webs/3/linker$ ` – Michael Durrant Jan 16 '12 at 23:26
  • 1
    If you then are required to have a javascript library installed... gem install 'execjs' and gem install 'therubyracer' – Nick Woodhams Apr 26 '12 at 10:30
  • @TravisR: Thanks for the update, I just turned this into a community wiki to make it easier for everyone to keep it up to date. – mu is too short Jun 06 '12 at 21:00
  • I am using Ubuntu 12.10, I am not able to install pg gem after installing libpq-dev – vedarthk Jan 23 '13 at 10:37
  • @vedarthk: What does "not able" mean? Perhaps a new question with the install logs would get you the help you need. – mu is too short Jan 23 '13 at 18:18
  • @muistooshort Yes I have posted the question : http://stackoverflow.com/q/14477825/1277376 – vedarthk Jan 24 '13 at 09:43
  • Works with Ubuntu 12.04 64 bit – Jason Kim Sep 05 '13 at 08:46
  • In `Mac OS X`, get `homebrew` from http://brew.sh , then `brew install postgres` to install PostgresQL and its header files. – Hanxue Nov 25 '13 at 06:31
  • Works like a charm in `centOS 6.5` – Sebastialonso May 14 '14 at 03:27
  • 2
    Please revise your answer to include `apt-get install postgres-server-dev-{pg.version}` Your answer no longer works with postgresql 9.4 on Ubuntu 14.04 See: http://stackoverflow.com/a/28837453/75194 – Ryan Rauh Mar 19 '15 at 19:42
  • @RyanRauh: Can you submit an edit? I'm not using Ubuntu right now so I'm not really qualified to say much here. – mu is too short Mar 20 '15 at 03:58
  • I just had a probelm with this because I was confusing g and q. to be clear, the package wanted is `libpq-dev` with a Q not a G – Shelvacu Nov 06 '15 at 22:32
  • I tried to do that on Ubuntu 14.04 but it gave me this: " libpq-dev : Depends: libpq5 (= 9.3.4-1) but 9.3.10-0ubuntu0.14.04 is to be installed. – Michael Lafayette Jan 13 '16 at 13:49
  • But then when I try to install libpq5, it says "libpq5 is already the newest version. Same for PostgreSQL." – Michael Lafayette Jan 13 '16 at 13:50
  • PostgreSQL version is: psql --version: psql (PostgreSQL) 9.3.10. PostgreSQL 9.3.10 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2, 64-bit. – Michael Lafayette Jan 13 '16 at 13:51
  • The following packages have unmet dependencies: postgresql-server-dev-9.3 : Depends: libpq-dev (>= 9.3~) but it is not going to be installed E: Unable to correct problems, you have held broken packages. – Michael Lafayette Jan 13 '16 at 13:51
  • @MichaelLafayette I don't have Ubuntu set up anywhere right now so I don't know what the problem could be. – mu is too short Jan 13 '16 at 18:08
  • 1
    While I executed the command : sudo apt-get install libpq-dev, I get a response - "Unable to locate package". So I execute sudo apt-get update, then executed "sudo apt-get install libpq-dev". Finally it successfully installed. – jamesxu-e.g. Apr 11 '17 at 07:13
  • Why do gem designers rely on implicit, undocumented, globally installed dependencies, and make no attempt to check for their existence before installing the gem? – Andy Ray Oct 18 '18 at 23:37
  • @AndyRay But they are checking for dependencies, that's what "checking for pg_config" and "checking for libpq-fe.h" are about. There's no way to check if the PostgreSQL development stuff is installed without, well, installing some scripts that look for the standard PostgreSQL dev tools (such as `pg_config` or the header file). Same thing happens when you need anything that isn't packaged inside a gem. – mu is too short Oct 19 '18 at 00:03
  • Ubuntu/Debian systems solution valid for Raspbian v9.9 on Raspberry Pi devices. – Sopalajo de Arrierez Aug 13 '19 at 14:32
  • I tried the same steps but still getting error in Redhat: Building native extensions. This could take a while... ERROR: Error installing pg: ERROR: Failed to build gem native extension. current directory: /opt/chef/embedded/lib/ruby/gems/2.5.0/gems/pg-1.1.4/ext /opt/chef/embedded/bin/ruby -r ./siteconf20190924-18624-nckmb.rb extconf.rb checking for pg_config... yes Using config values from /bin/pg_config checking for libpq-fe.h... *** extconf.rb failed *** – Jaydeep Sep 24 '19 at 10:04
  • 1
    This worked for me, I already had installed Postgres, and I just needed to execute `sudo apt-get install libpq-dev` then `bundle install` and the error disappeared. Thanks – Chris Vilches Oct 20 '19 at 17:00
239

On macOS (previously Mac OS X and OS X), use Homebrew to install the proper headers:

brew install postgresql

and then running

gem install pg

should work.

Alternatively, instead of installing the whole postgresql, you can brew install libpq and export the correct PATH and PKG_CONFIG_PATH as explained in the 'Caveats' section

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
  • 9
    Does this cause any problems if you're also using Postgres.app? – iconoclast Oct 30 '14 at 17:48
  • 1
    @iconoclast I use Postgres.app to run a db instance so having the `pg` gem just allows you to hook your Rails app up to your local Postgres.app instance – iwasrobbed Oct 31 '14 at 05:50
  • 3
    For me after run `brew install postgressql` i run `ARCHFLAGS="-arch x86_64" gem install pg` and works fine. – overallduka Oct 31 '14 at 17:05
  • 6
    Alternatively, just running `brew install libpqxx` on mac OSX suffices – Ricardo Saporta Jan 21 '15 at 08:12
  • 4
    @RicardoSaporta `libpqxx` installs `postgresql` as dependency, so it is essentially the same as `brew install postgresql` – Troggy Jun 12 '17 at 18:47
  • 7
    Instead of installing the whole `postgresql`, you can `brew install libpq` and export the correct `PATH` and `PKG_CONFIG_PATH` as explained in the 'Caveats' section. – goetz Jan 14 '19 at 16:33
  • This also sorted it for me on Ubuntu 20.04 using Linuxbrew. `sudo apt-get install libpq-dev` kept failing because of some other dependency – 8bitme Oct 02 '20 at 10:09
  • As someone who's just been hired to work with Ruby/Rails for the first time... Are there many more issues like this? Where a build requires a **database** installation to run? Do Ruby builds keep breaking for random reasons like this? – shikharraje Oct 19 '21 at 08:40
  • @iconoclast I can confirm this does not cause problems with Postgres.app. I've installed Postgres.app and done 'brew install postgres' on two Macs now, in order to get around the problem of pg not installing due to missing headers. Both times worked great, and allow successful bundle install or gem install pg. No need for specifying --with-pg-config, that was a red herring for me. – jdempcy Jan 23 '22 at 03:24
128

I had also tried doing gem install libpq-dev, but I received this error:

Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

However I found that installing with sudo apt-get (which I try to avoid using with Ruby on Rails) worked, i.e.

sudo apt-get install libpq-dev
# or
apt-get install postgres-server-dev-{pg.version}
# for postgresql 9.4 on Ubuntu 14.04

then I was able to do

gem install pg

without issues.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
57

I could solve this in another way. I didn't find the library on my system. Thus I installed it using an app from PostgreSQL main website. In my case (OS X) I found the file under /Library/PostgreSQL/9.1/include/ once the installation was over. You may also have the file somewhere else depending on your system if you already have PostgreSQL installed.

Thanks to this link on how to add an additional path for gem installation, I could point the gem to the lib with this command:

export CONFIGURE_ARGS="with-pg-include=/Library/PostgreSQL/9.1/include/"
gem install pg

After that, it works, because it now knows where to find the missing library. Just replace the path with the right location for your libpq-fe.h

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Pelaez
  • 1,374
  • 1
  • 13
  • 16
  • 2
    David's solution worked for me. I tracked libpq-fe.h to '/Applications/Postgres.app/Contents/Versions/9.3/include' and used the export command with said path, followed by the 'gem install pg' and the gem installed successfully. – Ryan Spears Apr 11 '14 at 12:13
  • Thanks for this, worked for me, using: `export CONFIGURE_ARGS="with-pg-include=/usr/local/Cellar/postgresql/9.3.1/include/"` – brookr Jul 31 '14 at 19:32
  • 1
    Using Postgresql 9.3 on CentOS 6 I solved this by symlinking the `pg_*` scripts into the $PATH like so: `ln -s /usr/pgsql-9.3/bin/p* /usr/local/bin`. I installed postgres 9.3 like so: `yum install http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-1.noarch.rpm && yum install postgresql93 postgresql93-contrib postgresql93-server postgresql93-devel` – fearmint Jan 16 '15 at 18:52
  • 2
    you can also write as `gem install pg -- with-pg-include=/Library/PostgreSQL/9.1/include/` – random-forest-cat Mar 02 '15 at 00:52
  • 1
    Better yet, use `export CONFIGURE_ARGS="with-pg-config=/path/to/postgres/bin/pg_config"`. The `pg_config` executable will be called to get all the required compiler and linker options — you can run it yourself to see them. – skozin Apr 19 '19 at 14:36
  • David's version didn't work for installing an old version of the gem (0.21), but @skozin's instruction did. Thank you! – johnpitchko Aug 14 '21 at 15:45
  • this worked perfectly for me on ubuntu 20.04 LTS. Thank you! – Exodus Reed Sep 03 '21 at 10:38
33

Can't find the libpq-fe.h header

i had success on CentOS 7.0.1406 running the following commands:

~ % psql --version # => psql (PostgreSQL) 9.4.1
yum install libpqxx-devel
gem install pg -- --with-pg-config=/usr/pgsql-9.4/bin/pg_config

Alternatively, you can configure bundler to always install pg with these options (helpful for running bundler in deploy environments),

  • bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config
random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
  • 1
    very helpful, thanks. Just to add a note regarding bundle config... you must run this command while logged-in as the user who will be running the subsequent bundle command. The bundler config is stored in ~/.bundle/config, so it will not be found if you run bundle config while logged-in as root, but bundler is being run by (e.g. with capistrano) the 'deploy' user. – Les Nightingill May 28 '15 at 12:36
  • 3
    **bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config** solve i for me on Centos 7 – liloargana Jan 03 '16 at 19:39
  • 2
    ***bundle config build.pg --with-pg-config=/usr/pgsql-10/bin/pg_config*** if you are using postgresql10 – Mahesh Neelakanta Feb 09 '18 at 20:33
  • For postgres 12 and above I had to do `yum install libpq5-devel` – Phil Mar 10 '22 at 10:05
26

For CentOS 6.4,

yum install postgresql-devel
gem install pg

worked well!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arivarasan L
  • 9,538
  • 2
  • 37
  • 47
19

Just for the record:

Ruby on Rails 4 application in OS X with PostgresApp (in this case 0.17.1 version needed - kind of an old project):

gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pablomarti
  • 2,087
  • 2
  • 22
  • 35
  • 1
    Works great if you're using the wonderful [Postgres.app](https://postgresapp.com). For Postgres 10, I used the path `/Applications/Postgres.app/Contents/Versions/10/bin/pg_config` – Master of Ducks Dec 16 '17 at 21:23
18

It is only issue of missing libpq-fe.h

On ubuntu we just need to run: sudo apt-get install libpq-dev

Above command will install a NEW package libpq-dev and after that you can again execute bundle install to resume your bundle installation.

Yash Dubey
  • 231
  • 2
  • 3
16

On Mac OS X run like this:

gem install pg -- --with-pg-config=***/path/to/pg_config***

***/path/to/pg_config*** is path to pg_config

Jiemurat
  • 1,619
  • 20
  • 20
  • 2
    Upvoted. I installed Postgresql via EnterpriseDB's [grapihcal installer](http://www.enterprisedb.com/products-services-training/pgdownload#osx). Then I need to find out the pg-config path by `sudo find / -name "pg_config"`, then do `gem install pg -- --with-pg-config=/Library/PostgreSQL/9.4/bin/pg_config` using the path I have just found out. – Yi Zeng Dec 31 '14 at 21:56
14

In my case it was package postgresql-server-dev-8.4 (I am on Ubuntu 11.04 (Natty Narwhal), 64 bits).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Johann
  • 391
  • 3
  • 5
13

For MacOS without installing PostgreSQL server:

brew install libpq
gem install pg -- --with-pg-config="/usr/local/Cellar/libpq/9.6.6/bin/pg_config"
Michael A.
  • 1,071
  • 12
  • 21
12

My solution for Fedora 30:

sudo dnf install /usr/include/libpq-fe.h
Aaron Castro
  • 359
  • 3
  • 12
10

The right answer for Mac users with Postgres.app is to build against the libpq provided with that package. For example, with the 9.4 release (current as of this writing), all you need is:

export CONFIGURE_ARGS="with-pg-include=/Applications/Postgres.app/Contents/Versions/9.4/include"
gem install pg

This will keep your pg gem in sync with exactly the version of PostgreSQL you have installed. Installing something from Homebrew is a waste in this case.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jelder
  • 2,220
  • 2
  • 19
  • 17
9

I had the same issue on Amazon Linux. I could find the header libpq-fe.h, but somehow it didn't work.

It came from the different versions of the packages that were installed through the different users on the machine. PostgreSQL 9.2 and PostgreSQL 9.3 were installed. So, make sure of your PostgreSQL version before including the libraries.

For me, the magic command line was:

sudo yum install postgresql93 postgresql93-server postgresql93-libs postgresql93-contrib postgresql93-devel

Source: An almost idiot's guide to install PostgreSQL 9.3, PostGIS 2.1 and pgRouting with Yum

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fab V.
  • 1,052
  • 12
  • 17
  • After installing libraries... do a clean up: "make distclean", "make clean", "./configure" and start compiling! – mzalazar Feb 05 '17 at 23:18
8

A more general answer for any Debian-based distribution (which includes Ubuntu) is the following. First, install the apt-file package running as root:

apt-get install apt-file

This allows you to search for packages containing a file. Then, update its database using

apt-file update

(this can be run as normal user). Then, look for the missing header using:

apt-file search libpq-fe.h

On my machine, this gives:

libpq-dev: /usr/include/postgresql/libpq-fe.h
postgres-xc-server-dev: /usr/include/postgres-xc/server/gtm/libpq-fe.h

There you go !

Vincent Fourmond
  • 3,038
  • 1
  • 22
  • 24
8

On Ubuntu, install the "libpq-dev" to get rid of this issue.

sudo apt-get install libpq-dev
Raju
  • 119
  • 1
  • 3
8

On debian bullseye, none of the existing answers solved the issue for me.

In case it helps someone with the same configuration as mine:

  1. Use PostgreSQL's apt repository: https://wiki.postgresql.org/wiki/Apt
  2. sudo apt-get update && sudo apt-get dist-upgrade (upgrade PostgreSQL from v13 to v14)
  3. sudo apt-get install libpq-dev
  4. gem install pg or bundle install
yoones
  • 2,394
  • 1
  • 16
  • 20
7

On Fedora 32, I managed to solve this same issue by installing libpq5-devel specifically.

If you install postgresql from the official repository, you will have several versions that won't fix the problem, so it's really a matter of trying out which one it needs.

A. Rosas
  • 171
  • 2
  • 5
6

I had this issue with Postgresql 9.6. I was able to fix it by doing:

brew upgrade postgresql@9.6
brew link postgresql@9.6 --force
gem install pg
mwalsher
  • 2,790
  • 2
  • 33
  • 41
5

I had the same problem on Mac OS, but I installed the PostgreSQL gem easily by using the following in a terminal:

ARCHFLAGS="-arch x86_64" gem install pg

(I installed PostgreSQL first with brew install postgresql.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rassom
  • 2,896
  • 5
  • 34
  • 45
5

I found this answer, and it was the only one that worked for me (Mac OS) - after researching for about two days:

$ sudo su

$ env ARCHFLAGS="-arch x86_64" gem install pg

Building native extensions.  This could take a while...
Successfully installed pg-0.11.0
1 gem installed
Installing ri documentation for pg-0.11.0...
Installing RDoc documentation for pg-0.11.0...

See Stack Overflow question Can't find the PostgreSQL client library (libpq).

Community
  • 1
  • 1
Cynthia Sanchez
  • 178
  • 1
  • 9
5

I recently upgraded to Mac OS X v10.10 (Yosemite) and was having difficulty building the pg gem.

The error reported was the typical:

Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... *** extconf.rb failed ***

My solution was to gem uninstall pg and then bundle update pg to replace the gem with the latest. I did run brew update; brew upgrade after the Yosemite install to get the latest versions of packages I had installed previously.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martin Streicher
  • 1,983
  • 1
  • 18
  • 18
5

On a Mac, I solved it using this code:

gem install pg -v '0.18.4' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mario
  • 847
  • 1
  • 8
  • 13
4

I am running Postgres.app on a Mac and I had to

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin

first. Then

bundle install

worked for me.

joh-mue
  • 1,601
  • 13
  • 20
4

I encountered the same error with postgres installed through asdf. The pg-config solutions didn't work for me. Instead I had to locate the postgres include folder which contains the file and run the command with the --with-pg-include flag

gem install pg -- --with-pg-include=/<path>/.asdf/installs/postgres/<version>/include
Matthias Michael Engh
  • 1,159
  • 2
  • 10
  • 25
4

Solved on MacOS M1:

brew install libpq

Then I put in my PATH

echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

And:

gem install pg
3

On CentOS,I installed libpq-dev package using below command

yum install postgresql-devel

Executing gem install pg returned the same error as "No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config".

Installing the gem as below solved my problem

gem install pg -- --with-pg-config=/usr/pgsql-x.x/bin/pg_config
user2086641
  • 4,331
  • 13
  • 56
  • 96
3

The location of libpq-fe.h depends on where your PostgreSQL install is (which depends on how you installed it). Use locate (http://en.wikipedia.org/wiki/Locate_%28Unix%29) to find the libpq-fe.h file on your machine. If it exists, it will be in the include directory of your PostgreSQL install.

$ locate libpq-fe.h
/Library/PostgreSQL/9.1/include/libpq-fe.h

The bin directory, which contains pg_config, will be in the same directory as the include directory. As the error suggests, use the --with-pg-config option to install the gem:

$ gem install pg --with-pg-config="/Library/PostgreSQL/9.1/bin/pg_config"

Note that you may need to run updatedb if you have never used locate or if you haven't updated since installing PostgreSQL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
akbrown
  • 41
  • 1
  • Without a `--` before the `--with-pg-config=...` I get an error: _ERROR: While executing gem ... (OptionParser::InvalidOption) invalid option: --with-pg-config=/usr/bin/pg_config)_ – Alexis Wilke Oct 15 '19 at 05:17
3

On Ubuntu 20.04, I already had libpq5:amd64 version 13.2-1.pgdg20.04+1. Now when I tried to install libpq-dev from apt repository, I was receiving

The following packages have unmet dependencies:
libpq-dev : Depends: libpq5 (= 12.6-0ubuntu0.20.04.1) but 13.2-1.pgdg20.04+1 is to be installed

I grabbed the deb from http://ftp.us.debian.org/debian/pool/main/p/postgresql-13/libpq-dev_13.2-1_amd64.deb and did the following:

mkdir -p /tmp/libpq-dev
dpkg-deb -R libpq-dev_13.2-1_amd64.deb /tmp/libpq-dev
sed -i 's|= 13.2-1|= 13.2-1.pgdg20.04+1|' /tmp/libpq-dev/DEBIAN/control
dpkg-deb -b /tmp/libpq-dev /tmp/libpq-dev-new.deb
sudo dpkg -i /tmp/libpq-dev-new.deb

I have been using the gem happily after that. No additional installs or anything.

Samar
  • 1,865
  • 12
  • 13
3

For alpine, please use below to fix the error

apk add --no-cache postgresql-dev
Chinmaya Biswal
  • 457
  • 5
  • 13
3

I solved it by creating symlink on MacOS Ventura

if postgresql already installed;

brew link postgresql@12 --force

This command will give directory path as below

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/opt/homebrew/opt/postgresql@12/bin:$PATH"' >> ~/.zshrc

you need to add zsh or bash_profile to your computer, whichever you are using

2

On FreeBSD (9.1) the necessary package is /usr/ports/database/postgresql-server* which when installed will also install the required header file that makes the gem install of "pg" fail. This answer here helped me find the solution but the difference in package names required a bit of searching.

Hopefully this helps someone avoid a bit of head scratching when searching for the "-dev" package on a FreeBSD system!

Claus Due
  • 4,166
  • 11
  • 23
2

On Debian 7.0, 64-bit (Wheezy), just run:

sudo apt-get install libpq-dev

After you successfully installed libpq-dev, run:

bundle install
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric Wu
  • 766
  • 6
  • 10
2

Under CentOS 6.5 (Squeeze) I created a file:

$ sudo touch /etc/profile.d/psql.sh

with content:

pathmunge /usr/pgsql-9.3/bin

Note here, you should set your PostgreSQL path with the pg_config file. You can find this with the command:

$ sudo find / -iname pg_config

Save the file:

$ sudo chmod +x /etc/profile.d/ruby.sh

and try to execute your command again.

Note: Anytime you change the Bash configuration - changing the profile.d configuration - you should reload Bash.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lucianosousa
  • 8,144
  • 4
  • 21
  • 25
  • How did you install postgres? I symlinked the `pg_*` scripts into the path like so: `ln -s /usr/pgsql-9.3/bin/p* /usr/local/bin`. After that I didn’t need any additional scripts or environment variables. – fearmint Jan 16 '15 at 18:55
2

I finally solved this problem, but not using the previously described methods.

Using brew install postgresql, I find out that it had already been installed, but not linked.

  1. Find out where PostgreSQL is installed, and delete it,

  2. Then brew install postgresql again,

  3. brew link postgresql

  4. gem install pg

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chile Yang
  • 21
  • 2
  • "brew link --force postgresql" worked for me but befor that i have to run 'export PATH="/usr/local/opt/postgresql@9.4/bin:$PATH"' >> ~/.bash_profile – Jagdish Aug 01 '18 at 15:14
1

I solved this installing the 'postgresql-common' package. This package provides the pg_config binary which is most likely what you were missing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fabrizio Regini
  • 1,500
  • 13
  • 26
1

I was having a similar problem, and this fixed it for me:

gem install do_postgres -- --with-pgsql-server-dir=/Applications/Postgres.app/Contents/MacOS --with-pgsql-server-include=/Applications/Postgres.app/Contents/MacOS/include/server

Source:

https://gist.github.com/oisin/6562181

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dunkles
  • 11
  • 1
1

On OS X 10.9 (Mavericks), I brew-installed postgresql, and I then had to rvm reinstall my Ruby. I am happy now :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ether_joe
  • 1,068
  • 1
  • 13
  • 32
1

For AltLinux the package postgresqlx.x-devel (in my case postgresql9.5-devel) must be installed:

apt-get install postgresql9.5-devel
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
  • You can also install specific versions with yum - yum install postgresqlxx-devel e.g yum install postgresql94-devel – dworrad Jul 18 '16 at 09:12
1

I just had this on OSX running brew and postgres@9.4.

My fix was this:

CONFIGURE_ARGS="with-pg-include=/usr/local/opt/postgresql@9.4/include/" bundle install
Nick
  • 2,803
  • 1
  • 39
  • 59
1

For Mac OS X just install https://postgresapp.com/ and follow steps for command line section and ruby

sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

Then

sudo ARCHFLAGS="-arch x86_64" gem install pg
Muhammad Hassan Nasr
  • 2,718
  • 3
  • 18
  • 20
  • I found the same answer here for the top code section above: https://postgresapp.com/documentation/cli-tools.html under "Configure your $PATH" then all I had to do was open a new terminal and ```bundle install```. I'm on an M1 Mac. – datadaveshin Aug 19 '23 at 01:57
1

I was able to get it working on MacOS Ventura with Apple Silicon by

brew install libpq
brew install postgresql

gem install pg -- --with-pg-config=/opt/homebrew/Cellar/postgresql@14/14.7/bin/pg_config

Check and replace the /postgresql@14/14.7/ part to match your version.

user9869932
  • 6,571
  • 3
  • 55
  • 49
0

Only uninstalling (sudo apt-get purge) libpq-dev and re-installing it worked for me.

vasilakisfil
  • 2,279
  • 4
  • 24
  • 31
0

On OS X and MacPorts the -devel package for PostgreSQL is not required any more. Installing the pg gem works as follows:

~ > sudo port install postgresql-devel
...
Error: postgresql-devel has been replaced by postgresql91; please install that port instead
~ > gem install pg -v '0.17.1' -- --with-pg-config=/opt/local/lib/postgresql95/bin/pg_config

See also this post.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jens
  • 8,423
  • 9
  • 58
  • 78
  • `port select --set postgresql postgresql96` or any other version you installed to active symlinks including `pg_config` – troex Feb 13 '17 at 22:59
0

On Arch Linux you will need to install postgresql-libs:

sudo pacman -Syu postgresql-libs
Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
0

Step 1) Make sure postgress is installed in your system ( If it's already installed and you can run postgress server on your machine move to step (2)

apt-get install postgresql postgresql-contrib (sol - fatal error: libpq-fe.h: No such file or directory)
sudo apt-get install ruby-dev (required to install postgress below)
sudo gem install pg
sudo service postgresql restart

Step 2) Some of the c++ files are trying to access libpq-fe.h directly and cant find. So we need to manually search every such file and replace libpq-fe.h with postgresql/libpq-fe.h

Command for searching all occurrences of libpq-fe.h in all dir and subdir is grep -rnw ./ -e 'libpq-fe.h'

3) Go to All the file listed by command run in step 2 and manually change libpq-fe.h with postgresql/libpq-fe.h.

sapy
  • 8,952
  • 7
  • 49
  • 60
0

I have tried all solutions but only works below command

pip install psycopg2-binary
RaviPatidar
  • 1,438
  • 1
  • 18
  • 29
0

Ubuntu 20.04.1 LTS, this seemed to work for me:

sudo apt-get install ruby ruby-dev libpqxx-dev libpq-fe-dev libpq-dev 
sudo gem install pg -- --with-pg-config=/usr/include/postgresql/pg_config --with-pg-include=/usr/include/postgresql/
Roel Van de Paar
  • 2,111
  • 1
  • 24
  • 38
  • And `ls /usr/include/postgresql/libpq-fe.h /usr/include/postgresql/pg_config` can be used to check the files are there to start with. – Roel Van de Paar Aug 28 '20 at 04:41
-2

I fixed the same error by doing a Ruby reinstall via rvm:

rvm reinstall 1.9.3
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131