pg gem install fails on M1 mac: "error: conflicting types for 'PQconnectdb'" - homebrew

I'm installing the pg gem on mac osx 12.5 on an M1 macbook pro. Ruby bundler can't find libpq when running gem install pg:
current directory: /Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/pg-1.4.4/ext
/Users/me/.rbenv/versions/3.1.2/bin/ruby -I /Users/me/.rbenv/versions/3.1.2/lib/ruby/site_ruby/3.1.0 extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... yes
Using config values from /opt/homebrew/opt/libpq/bin/pg_config
checking for whether -Wl,-rpath,/opt/homebrew/opt/libpq/lib is accepted as LDFLAGS... yes
Using libpq from /opt/homebrew/opt/libpq/lib
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)
*****************************************************************************
Unable to find PostgreSQL client library.
Please install libpq or postgresql client package like so:
brew install libpq
or try again with:
gem install pg -- --with-pg-config=/path/to/pg_config
or set library paths manually with:
gem install pg -- --with-pg-include=/path/to/libpq-fe.h/ --with-pg-lib=/path/to/libpq.so/
...
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/me/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/extensions/x86_64-darwin-21/3.1.0/pg-1.4.4/mkmf.log
Here's what the mkmf.log shows:
conftest.c:16:13: error: conflicting types for 'PQconnectdb'
extern void PQconnectdb();
^
/opt/homebrew/include/postgresql#14/libpq-fe.h:285:16: note: previous declaration is here
extern PGconn *PQconnectdb(const char *conninfo);
^
conftest.c:17:27: error: too few arguments to function call, single argument 'conninfo' was not specified
int t(void) { PQconnectdb(); return 0; }
~~~~~~~~~~~ ^
/opt/homebrew/include/postgresql#14/libpq-fe.h:285:16: note: 'PQconnectdb' declared here
extern PGconn *PQconnectdb(const char *conninfo);
I've installed libpq using homebrew as suggested:
Warning: libpq 15.1 is already installed and up-to-date.
Interestingly, I also tried gem install pg -- --with-pg-include=/opt/homebrew/opt/libpq/include/libpq-fe.h/ but I couldn't find libpq.so poking around the homebrew install.
Any suggestions? Is it possible homebrew's installation of libpq leaves off a needed .so file? Is it suspect that the log file is under a x86_64-darwin-21 directory when I'm running on an M1 mac? Is error: conflicting types for 'PQconnectdb' a hint that I have an incompatible version of libpq with the pg gem?

The pg gem uses pg_config to configure itself. If the gem finds the wrong version of pg_config, it will misconfigure, resulting in the error you mentioned.
On ARM Macs, Homebrew installs itself to a different location (/opt/homebrew) than it did on Intel Macs (/usr/local). However, if you used Apple's Migration Assistant, it's possible to end up with both Homebrew instances, which can confuse things when compiling gems.
First, make sure your Ruby is arm64:
$ ruby -v
ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [arm64-darwin22]
It should show arm64 there near the end.
Next, see where pg_config is coming from:
$ which pg_config
/usr/local/bin/pg_config <- Intel version
/opt/homebrew/bin/pg_config <- ARM version
It'll show one of these two (or nothing). We're looking for the second line (ARM).
If nothing, then postgresql is probably not installed (or not found).
If it shows the first, then compilation is finding an Intel copy of pg_config. This was my case, after migrating from an Intel to ARM Mac.
This confusion is partly made possible because pg_config comes from the postgresql package, not libpq. So, you'll need the arm64 version of postgresql installed too.
If you install the unversioned (ie: latest) postgresql, this is hopefully all you need:
$ brew install postgresql
If you always install a specific version (as I do), then you'll also need to 'link' so that pg_config is properly made available.
$ brew install postgresql#15
$ brew link postgresql#15
Using the link command is usually safe as long as this is the only version of postgresql you have installed (within the ARM copy of Homebrew).
Hint 1: When upgrading versions of Postgres in the future, you may need to unlink the old before linking the new.
Hint 2: If you have an Intel version of Postgres still running from the old copy of Homebrew, there is a possibility you'll have to migrate your data between the two or sort out some other conflict. That's out of scope here, so just a heads up.
At this point you will hopefully see the proper pg_config:
$ which pg_config
/opt/homebrew/bin/pg_config <- ARM version
If not, also check your PATH as set by your shell. It's important that /opt/homebrew/bin is before /usr/local/bin.
$ echo $PATH
Fixing it depends on your choice of shell, so I'll skip that here.
That should hopefully do it.
Note that I didn't use --with-pg-config. While using that might allow skipping a step or two above, getting pg_config detecting properly also helps improve odds that other gems will compile properly in the future and not need ongoing workarounds.

Related

Install PG gem in OpenSUSE

283
65
I'm trying to install PostgreSQL's pg gem for Ruby.
I issued the following command:
gem install pg
I installed Ruby 1.9.2 using RVM.
The above command shows me the following error.
The error is :
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/home/User/.rvm/rubies/ruby-1.9.2-preview3/bin/ruby extconf.rb
checking for pg_config... yes
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.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 ***
etc...
Somebody gave solution for Ubuntu users.
What about OpenSUSE Linux?
Thanks for advance.
For me this was the solution:
zypper in postgresql-server-devel
pg_config is part of postgresql-server-devel, if you install that
package then it should work on Tumbleweed at least.
Source: https://lists.opensuse.org/opensuse-factory/2019-03/msg00300.html
As per my search OpenSUSE linux has no package for client of Postgres, but as you have installed that it, that mean chances are the file pgsql.so is present in your system. How did you install it, I can see following which says it will install development libraries and client also
sudo zypper in postgresql postgresql-server postgresql-contrib
sudo zypper in postgresql-plperl postgresql-plpython postgresql-plctl
If this does not work then try to search in postgres installation path this file, locate file_name is easy to find files on different linux, but not sure it works in openSUSE it is part of mlocate package. If you find that file then symbol link it as per error, where error is searching this file.
If it is still not working, then you can use your Postgres directory in environment variable and try to install gem individually. To get postgres configuration use following command to see directories.
pg_config
Now try to install gem
PATH=$PATH:/Library/PostgreSQL/9.0/bin sudo gem install pg
On some system following command also resolve issue, so you can try it too
sudo gem install pg – --with-pg-config=/usr/local/pgsql/bin/pg_config

Update ruby version on MAC

I'm trying to update ma ruby version.
I'm with ruby 1.8.7 and I would like to install 2.1.0.
I just installed rvm, rbenv, brew and gem.
But when I try to do
$ rbenv install 2.1.0.
I get an error about clang. I don't get the point between clang and ruby
Could you please help me?
Error:
$ rbenv install 2.1.0
Downloading ruby-2.1.0.tar.gz...
Installing ruby-2.1.0...
BUILD FAILED
...
Last 10 log lines:
...
checking build system type... x86_64-apple-darwin12.3.0
checking host system type... x86_64-apple-darwin12.3.0
checking target system type... x86_64-apple-darwin12.3.0
checking for gcc-4.2... no
checking for clang... no
checking for gcc... no
checking for cc... no
./configure: line 3390: -E: command not found
configure: error: clang version 3.0 or later is required
You need to install the Developer Tools command-line tools in order to build Ruby. As one option, you can install Xcode from the App Store, and Xcode will let you install the command line tools if they aren't automatically installed with your version.
You can install developer command line tools without installing Xcode. Just type the following in the terminal:
xcode-select --install

pg gem install fails saying version is too old?

I am trying to install the pg (PostreSQL) gem for Ruby. I am receiving this error:
postgres/9.2-pgdg/bin/64/pg_config
Using config values from /location/to/install/postgres/9.2-pgdg/bin/64/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... yes
checking for PQconnectionUsedPassword()... no
Your PostgreSQL is too old. Either install an older version of this gem or upgrade your database.
*** 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.
PostgreSQL version: postgres (PostgreSQL) 9.2.3
OS: solaris 10
I have also tried:
gem install pg -- --with-pgsql-lib=/location/of/install/postgres/9.2-pgdg/lib/64/ --with-pg-config=/location/of/install/postgres/9.2-pgdg/bin/64/pg_config
I have no idea why this error is occurring and I cannot find any useful info on Google.
Any help is greatly appreciated.
Lots of things can go wrong installing a database client, the gem and using it.
I generally install PostgreSQL from source, not from a distro. At that point I know I have all the source and know exactly where things got installed. That's important when installing the gem that talks to the client drivers.
I also rely on installing Ruby from source, either by directly installing it myself, or by using rbenv or RVM if it's on one of my development boxes. Then I also install the pg gem directly, using gem install pg; I've had too many bad experiences using distros when installing languages so I go old-school on it.
I wrote a little script I use on my Mac OS systems, that I've found useful for my CentOS Linux boxes:
#!/bin/sh -x
PATH=/Library/PostgreSQL/9.2/bin:$PATH
gem install pg
I adjust the PATH addition depending on where PostgreSQL got installed, and it seems to do the trick. I've used the longer, more "comprehensive" options also, but this seems to work as well.
The issue is that the installer needs to dig out installation information from the pg_config executable and being able to find pg_config does the trick.
I was going nuts with this until I realized that postgresql-devel and postgresql-libs were from 8.1.23!!!
$ yum list installed postgres*
Loaded plugins: fastestmirror, security
Installed Packages
postgresql-devel.i386 8.1.23-10.el5_10 installed
postgresql-devel.x86_64 8.1.23-10.el5_10 installed
postgresql-libs.i386 8.1.23-10.el5_10 installed
postgresql-libs.x86_64 8.1.23-10.el5_10 installed
---------------
$ sudo yum install postgresql94-libs
$ sudo yum install postgresql94-devel
$ gem install pg
Successfully installed pg-0.18.1
Installing and linking postgresql from homebrew fixed it for me:
brew install postgresql
brew unlink postgresql91
brew link --overwrite postgresql

Error while building bundle due to pg gem? [duplicate]

I just upgraded from OS X Lion to OS X Mountain Lion.
My rails environment for the most part stayed in tact.
The only steps I had to do to get things back to a good state was:
- Install XCode 4.4
- Install XCode 4.4 Command Line Tools
- Install gcc-4.2
So all that went well.
Here's the issue, almost all my gems install properly when I run bundle install except for one of the most important ones I need - the postgresql gem 'pg'.
And it specifically fails for versions above 0.12.2.
I'm using ruby 1.9.3p125.
I can successfully grab the 0.12.2 pg gem, and versions below, but they give me segmentation faults (I'm guessing because of the version of ruby I'm using).
So this leads me to believe that it's specific to the pg gem, and not my overall environment seeing as other gems install fine.
I also tried re-installing Postgresql to ensure my postgres environment is ok and seems good on that front.
Has anybody been able to install the 'pg' gem version 0.13.0 or above (and more preferrably 0.14.X)?
Here is my stack trace:
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/caseyli/.rvm/rubies/ruby-1.9.3-p125/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/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... yes
checking for PQconnectionUsedPassword()... yes
checking for PQisthreadsafe()... yes
checking for PQprepare()... yes
checking for PQexecParams()... yes
checking for PQescapeString()... yes
checking for PQescapeStringConn()... yes
checking for PQescapeLiteral()... yes
checking for PQescapeIdentifier()... yes
checking for PQgetCancel()... yes
checking for lo_create()... yes
checking for pg_encoding_to_char()... yes
checking for pg_char_to_encoding()... yes
checking for PQsetClientEncoding()... yes
checking for PQlibVersion()... yes
checking for PQping()... yes
checking for rb_encdb_alias()... yes
checking for rb_enc_alias()... no
checking for PGRES_COPY_BOTH in libpq-fe.h... no
checking for struct pgNotify.extra in libpq-fe.h... yes
checking for unistd.h... yes
checking for ruby/st.h... yes
creating extconf.h
creating Makefile
make
compiling pg.c
pg.c: In function ‘pg_s_library_version’:
pg.c:273: warning: implicit declaration of function ‘PQlibVersion’
pg.c: In function ‘Init_pg_ext’:
pg.c:384: error: ‘PQPING_OK’ undeclared (first use in this function)
pg.c:384: error: (Each undeclared identifier is reported only once
pg.c:384: error: for each function it appears in.)
pg.c:386: error: ‘PQPING_REJECT’ undeclared (first use in this function)
pg.c:388: error: ‘PQPING_NO_RESPONSE’ undeclared (first use in this function)
pg.c:390: error: ‘PQPING_NO_ATTEMPT’ undeclared (first use in this function)
make: *** [pg.o] Error 1
Gem files will remain installed in /Users/caseyli/.rvm/gems/ruby-1.9.3- p125#elliottfarmequipment/gems/pg-0.14.0 for inspection.
Deefour's comment got it! It was the way I installed PostgreSQL.
I use the PostgreSQL one-click installer which I'm guessing doesn't set up the libpq up properly for Mountain Lion.
Homebrew on the otherhand will build it properly for the OS.
After installing postgres through homebrew
brew install postgresql
I was able to install my gem no problem.
Thanks everybody!
The issue is the location of pg_config. If you use the one-click installer, it is in /Library/PostgreSQL/9.2/bin/pg_config, so this will build your gem just fine:
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Library/PostgreSQL/9.2/bin/pg_config
I had the exact same issue; same stack trace as you've posted.
MRI 1.9.3-p286 / OS X 10.8.2 / Postgres 9.0.4 with homebrew.
I solved it by simply doing:
$ brew update
$ brew upgrade postgresql
$ bundle
Hope it works for someone else.
Reinstalling ruby fixed a similar issue I was having after upgrading to Mountain Lion
rvm reinstall 1.9.3
Note: also run the following as it seems the default version resets
rvm --default 1.9.3
For reference I posted the error I was seeing below
gem install pg
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb --with pg=/usr/local/Cellar/postgresql/9.1.4/bin
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Mac comes with an outdated prebuilt PostgreSQL client installation and pg_config file that comes with it conflicts with any new PostgreSQL server & client installations.
Installing ruby gem with a fully qualified file name of pg_config overrides default known location of pg_config and installation succeeds.
Current location of pg_config by one click installer by EnterpriseDB: /Library/PostgreSQL/bin/pg_config
This command finally worked for me.
CrashMX2$ gem install pg -- --with-pg-config=/Library/PostgreSQL/bin/pg_config
I have had the same problem but reinstalling postgresql with homebrew did not solve it. However, this command did it for me:
sudo env ARCHFLAGS="-arch x86_64" gem install pg
I was able to get it to work by installing the Postgres.app from Heroku and removing my existing postgresql installation. You can download the app here:
http://postgresapp.com/
brew remove postgresql
bundle install
If you didn't use brew, here's a link to more uninstall docs from Postgres.app
http://postgresapp.com/documentation#toc_16
I just got mine up and running via this ticket:
How can I pass a parameter for gem installation when I run bundle install?
bundle config build.pg --with-pg-config=/path/to/pg_config
bundle install
Like Matt, Ranjan, and Peter Mellett all say, you have to get the right pg_config location, and Mountain Lion comes with a different location that doesn't seem to play well with rubygems, so if you find your old version like
/Library/PostgreSQL/bin/pg_config
then you can set that in the bundle config before installing.
My situation was upgrading from Lion to Mountain Lion.
Homebrew installed pg_ctl in /usr/local/bin/pg_ctl, so the command for me was:
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_ctl
Fro those, who are using Kubuntu 13.04 and installed pg with one click tool, pg config is in other folder, so command:
gem install pg -- --with-pg-config=/opt/PostgreSQL/9.3/bin/pg_config

pg gem '0.14.0' on Mountain Lion fails

I just upgraded from OS X Lion to OS X Mountain Lion.
My rails environment for the most part stayed in tact.
The only steps I had to do to get things back to a good state was:
- Install XCode 4.4
- Install XCode 4.4 Command Line Tools
- Install gcc-4.2
So all that went well.
Here's the issue, almost all my gems install properly when I run bundle install except for one of the most important ones I need - the postgresql gem 'pg'.
And it specifically fails for versions above 0.12.2.
I'm using ruby 1.9.3p125.
I can successfully grab the 0.12.2 pg gem, and versions below, but they give me segmentation faults (I'm guessing because of the version of ruby I'm using).
So this leads me to believe that it's specific to the pg gem, and not my overall environment seeing as other gems install fine.
I also tried re-installing Postgresql to ensure my postgres environment is ok and seems good on that front.
Has anybody been able to install the 'pg' gem version 0.13.0 or above (and more preferrably 0.14.X)?
Here is my stack trace:
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/Users/caseyli/.rvm/rubies/ruby-1.9.3-p125/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/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... yes
checking for PQconnectionUsedPassword()... yes
checking for PQisthreadsafe()... yes
checking for PQprepare()... yes
checking for PQexecParams()... yes
checking for PQescapeString()... yes
checking for PQescapeStringConn()... yes
checking for PQescapeLiteral()... yes
checking for PQescapeIdentifier()... yes
checking for PQgetCancel()... yes
checking for lo_create()... yes
checking for pg_encoding_to_char()... yes
checking for pg_char_to_encoding()... yes
checking for PQsetClientEncoding()... yes
checking for PQlibVersion()... yes
checking for PQping()... yes
checking for rb_encdb_alias()... yes
checking for rb_enc_alias()... no
checking for PGRES_COPY_BOTH in libpq-fe.h... no
checking for struct pgNotify.extra in libpq-fe.h... yes
checking for unistd.h... yes
checking for ruby/st.h... yes
creating extconf.h
creating Makefile
make
compiling pg.c
pg.c: In function ‘pg_s_library_version’:
pg.c:273: warning: implicit declaration of function ‘PQlibVersion’
pg.c: In function ‘Init_pg_ext’:
pg.c:384: error: ‘PQPING_OK’ undeclared (first use in this function)
pg.c:384: error: (Each undeclared identifier is reported only once
pg.c:384: error: for each function it appears in.)
pg.c:386: error: ‘PQPING_REJECT’ undeclared (first use in this function)
pg.c:388: error: ‘PQPING_NO_RESPONSE’ undeclared (first use in this function)
pg.c:390: error: ‘PQPING_NO_ATTEMPT’ undeclared (first use in this function)
make: *** [pg.o] Error 1
Gem files will remain installed in /Users/caseyli/.rvm/gems/ruby-1.9.3- p125#elliottfarmequipment/gems/pg-0.14.0 for inspection.
Deefour's comment got it! It was the way I installed PostgreSQL.
I use the PostgreSQL one-click installer which I'm guessing doesn't set up the libpq up properly for Mountain Lion.
Homebrew on the otherhand will build it properly for the OS.
After installing postgres through homebrew
brew install postgresql
I was able to install my gem no problem.
Thanks everybody!
The issue is the location of pg_config. If you use the one-click installer, it is in /Library/PostgreSQL/9.2/bin/pg_config, so this will build your gem just fine:
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Library/PostgreSQL/9.2/bin/pg_config
I had the exact same issue; same stack trace as you've posted.
MRI 1.9.3-p286 / OS X 10.8.2 / Postgres 9.0.4 with homebrew.
I solved it by simply doing:
$ brew update
$ brew upgrade postgresql
$ bundle
Hope it works for someone else.
Reinstalling ruby fixed a similar issue I was having after upgrading to Mountain Lion
rvm reinstall 1.9.3
Note: also run the following as it seems the default version resets
rvm --default 1.9.3
For reference I posted the error I was seeing below
gem install pg
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb --with pg=/usr/local/Cellar/postgresql/9.1.4/bin
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Mac comes with an outdated prebuilt PostgreSQL client installation and pg_config file that comes with it conflicts with any new PostgreSQL server & client installations.
Installing ruby gem with a fully qualified file name of pg_config overrides default known location of pg_config and installation succeeds.
Current location of pg_config by one click installer by EnterpriseDB: /Library/PostgreSQL/bin/pg_config
This command finally worked for me.
CrashMX2$ gem install pg -- --with-pg-config=/Library/PostgreSQL/bin/pg_config
I have had the same problem but reinstalling postgresql with homebrew did not solve it. However, this command did it for me:
sudo env ARCHFLAGS="-arch x86_64" gem install pg
I was able to get it to work by installing the Postgres.app from Heroku and removing my existing postgresql installation. You can download the app here:
http://postgresapp.com/
brew remove postgresql
bundle install
If you didn't use brew, here's a link to more uninstall docs from Postgres.app
http://postgresapp.com/documentation#toc_16
I just got mine up and running via this ticket:
How can I pass a parameter for gem installation when I run bundle install?
bundle config build.pg --with-pg-config=/path/to/pg_config
bundle install
Like Matt, Ranjan, and Peter Mellett all say, you have to get the right pg_config location, and Mountain Lion comes with a different location that doesn't seem to play well with rubygems, so if you find your old version like
/Library/PostgreSQL/bin/pg_config
then you can set that in the bundle config before installing.
My situation was upgrading from Lion to Mountain Lion.
Homebrew installed pg_ctl in /usr/local/bin/pg_ctl, so the command for me was:
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_ctl
Fro those, who are using Kubuntu 13.04 and installed pg with one click tool, pg config is in other folder, so command:
gem install pg -- --with-pg-config=/opt/PostgreSQL/9.3/bin/pg_config

Resources