Skip to content

Instantly share code, notes, and snippets.

@tomholford
Last active September 7, 2025 13:07
Show Gist options
  • Select an option

  • Save tomholford/f38b85e2f06b3ddb9b4593e841c77c9e to your computer and use it in GitHub Desktop.

Select an option

Save tomholford/f38b85e2f06b3ddb9b4593e841c77c9e to your computer and use it in GitHub Desktop.
Install postgresql gem `pg` on macOS

Installing pg gem on macOS

If you're trying to install the postgresql gem pg and it is failing with the following error message:

Installing pg 1.2.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: ~/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/pg-1.2.3/ext
~/.rbenv/versions/3.0.0/bin/ruby -I ~/.rbenv/versions/3.0.0/lib/ruby/3.0.0 -r ./siteconf20210125-97201-pycpo.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

The following helped me resolve the issue without having to install the entire postgresql as recommended in the stop Stack Overflow post (already using it in a Docker container):

  1. brew install libpq
  2. gem install pg -- --with-pg-config=/usr/local/opt/libpq/bin/pg_config
@damuz91

damuz91 commented Sep 7, 2022

Copy link
Copy Markdown

bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

I confirm this made it to work, Apple M1 Max , thank you!

@EraliaKats

EraliaKats commented Sep 14, 2022

Copy link
Copy Markdown

gem install pg didn't work before I installed postgresql -> brew install postgresql added all the necessary headers for me

@Bartuz

Bartuz commented Sep 21, 2022

Copy link
Copy Markdown

For intel mac:

brew install libpq
bundle config build.pg -- --with-pg-config=/usr/local/opt/libpq/bin/pg_config

@darrenterhune

darrenterhune commented Sep 29, 2022

Copy link
Copy Markdown

Yo pg_config should be on your machine after a brew install postgresql so no need to do libpq or gem install dances

sudo find / -name "pg_config" -print
bundle config set build.pg --with-pg-config=/path/to/pg_config

Also symlink is not ideal but will work

@PeterTCormack

Copy link
Copy Markdown

In case anyone else had this problem, I saw the following error when I tried to install with the flag as mentioned here:

gem install pg -v '1.1.0' --source 'https://rubygems.org/' --with-pg-config=/usr/local/opt/libpq/bin/pg_config
ERROR:  While executing gem ... (OptionParser::InvalidOption)
    invalid option: --with-pg-config=/usr/local/opt/libpq/bin/pg_config

But, changing the bundle config as some have mentioned:

bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

worked like a charm on my Apple Silicon machine.

@fullstackplus

fullstackplus commented Oct 6, 2022

Copy link
Copy Markdown

I'm on Mac OS 12.6, and trying to locate pg_config. Running

sudo find / -name "pg_config" -print

resulted in a bunch of results like:

/usr/local/Cellar/libpq/14.5/bin/pg_config
/Library/PostgreSQL/14/debug_symbols/bin/pg_config.dSYM/Contents/Resources/DWARF/pg_config
/Library/PostgreSQL/14/bin/pg_config

I have Postgres installed with the installer package from their website, and not with brew install postgresql. So what's the path I need for installing the pg gem?

@fullstackplus

Copy link
Copy Markdown

Btw, when I installed libpq I had the following message:

libpq is keg-only, which means it was not symlinked into /usr/local,
because conflicts with postgres formula.

If you need to have libpq first in your PATH, run:
  echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> /Users/me/.bash_profile

@darrenterhune

Copy link
Copy Markdown

@fullstackplus uninstall libpq and then use the Cellar pg_config in the bundle config setting. You are seeing multiple because you have multiple versions of Postgres installed. The Library one is likely a system version or a version that maybe an App installed installed whereas the Cellar version is the homebrew version. You also need to make sure the Cellar version is in your PATH.

@fullstackplus

fullstackplus commented Oct 6, 2022

Copy link
Copy Markdown

@darrenterhune Thanks, weird that I have multiple versions of Postgres — I only installed it once. Would you recommend also uninstalling all Postgres versions except the Homebrew one?

Also, I just ran brew list and there's no Postgres formula! Only libpq.

@darrenterhune

Copy link
Copy Markdown

@fullstackplus I would definitely try and figure out how they go there and uninstall them. I have not used apps to install postgres before. It could be something like postgres.app or pg_admin installer, I'm not sure. I don't think any versions of mac os x come preloaded with postgres on them but 🤷‍♂️

If no versions of postgres from homebrew just uninstall libpq and then install it with homebrew.

@fullstackplus

fullstackplus commented Oct 6, 2022

Copy link
Copy Markdown

@darrenterhune Thanks man. I don't think Postgres came pre-installed with my Mac, running which postgres andwhich postgresql yields nothing, while running which sqlite3 yields:

/usr/bin/sqlite3

So I must have installed it indirectly somehow.

@monsieurnebo

Copy link
Copy Markdown

bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config fixed it for my MBP M1.

@chulkilee

Copy link
Copy Markdown
brew install libpq
bundle config build.pg --with-pg-config="$(brew --prefix)/opt/libpq/bin/pg_config"

You should not assume the brew installation path :)

@Startouf

Startouf commented Dec 30, 2022

Copy link
Copy Markdown
bundle config build.pg --with-pg-config="$(brew --prefix)/opt/libpq/bin/pg_config"

Works wonder ! One of the the cleanest answers so far !

@bradfeehan

Copy link
Copy Markdown

You can also use "${HOMEBREW_PREFIX}/opt/libpq/bin/pg_config" to avoid creating a new brew process.

@hueyAtFetchly

Copy link
Copy Markdown
bundle config build.pg --with-pg-config="$(brew --prefix)/opt/libpq/bin/pg_config"

Amazing, many other solutions didn't work (from fresh Ventura OS install).

@mikekavouras

Copy link
Copy Markdown

On Apple Silicon, after installing the libpq, running gem install pg -- --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

should do the trick.

@isheebo you just saved me 10000 hours I owe you everything

@alexbrahastoll

alexbrahastoll commented Jul 19, 2023

Copy link
Copy Markdown

What worked for me was the suggestion by @oamado:

gem install pg -v '0.18.4' -- --with-cflags="-Wno-error=implicit-function-declaration"

I am on an older Mac (MacBook Pro 2018) running macOS 12.5.1 (Monterey).

@StoreCarCharIsInNoSave

StoreCarCharIsInNoSave commented Aug 3, 2023

Copy link
Copy Markdown

To use with bundler / apple silicon:

brew install libpq
bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config
bundle

This worked for m1, Postgres.app. Thank you
But after that, psql may not work. The following helped me:
brew link --force libpq

@raaynaldo

Copy link
Copy Markdown

gem install pg -- --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

Works for me. Thanks!

@alexandrule

Copy link
Copy Markdown

To use with bundler / apple silicon:

brew install libpq
bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config
bundle

Thanks! It works!

@fabianoalmeida

Copy link
Copy Markdown

To use with bundler / apple silicon:

brew install libpq
bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config
bundle

Worked for me. I'm using Apple M1 Sonoma.

@hooopo

hooopo commented Jan 23, 2024

Copy link
Copy Markdown

gem install pg -- --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

You may need to do a user-level install depending on permissions.

 gem install pg --user-install -- --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

works for me.

@Rajat16nov

Copy link
Copy Markdown

Switching the ruby version worked for me
rbenv global 3.2.2
sudo chown -R $(whoami) ~/.rbenv
gem install pg

@antonengelhardt

Copy link
Copy Markdown

Merci!!

@KESEVAN

KESEVAN commented May 18, 2024

Copy link
Copy Markdown

gem install pg -- --with-pg-config=/usr/local/opt/libpq/bin/pg_config
worked for me! Using Apple M3 Pro

@dotneutron

Copy link
Copy Markdown

Got it to work with:

brew install libpq
export PKG_CONFIG_PATH="/opt/homebrew/opt/libpq/lib/pkgconfig"

for macOS Sonoma (Apple M2 Max).

@cjba7

cjba7 commented Dec 16, 2024

Copy link
Copy Markdown

100% clean install on a brand the just released Mac Mini M4 Pro.

Issue

...
Can't find the 'libpq-fe.h header

My resolve

brew install postgresql

@waruboy

waruboy commented May 28, 2025

Copy link
Copy Markdown
bundle config build.pg --with-pg-config="$(brew --prefix)/opt/libpq/bin/pg_config"

this works beautifully. Thanks! on M3

@okc0mputer

Copy link
Copy Markdown

For anyone else using Postgres.app: 'gem install pg -- --with-pg-config=/Applications/Postgres.app/Conten
ts/Versions/17/bin/pg_config' does the trick, just substitute your postgresql version number. No need to install libs from homebrew

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment