65
votes

I'm trying to install Psycopg2 on my Macbook, but I am getting an error. I found a lot of the same questions on StackOverflow but no answer seems to work.
I'm using:

OS: MacOS 10.9.5
Python Version: 3.4.3

My error code is:

Running setup.py egg_info for package psycopg2  Error: pg_config
executable not found.

Please add the directory containing pg_config to the PATH or specify
the full executable path with the option:

python setup.py build_ext --pg-config /path/to/pg_config build ...
 
or with the pg_config option in 'setup.cfg'.  Complete output from
command python setup.py egg_info:  running egg_info

writing pip-egg-info/psycopg2.egg-info/PKG-INFO
 
writing top-level names to
pip-egg-info/psycopg2.egg-info/top_level.txt
 
writing dependency_links to
pip-egg-info/psycopg2.egg-info/dependency_links.txt
 
warning: manifest_maker: standard file '-c' not found
 
Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH or specify the full executable path with the option:
 
python setup.py build_ext --pg-config /path/to/pg_config build ...
 
or with the pg_config option in 'setup.cfg'.
 
---------------------------------------- 
Command python setup.py egg_info failed with error code 1 in
/Users/sg/build/psycopg2 Storing complete log in
/Users/sg/Library/Logs/pip.log
15

15 Answers

88
votes

You don't seem to have postgres installed, check how to install postgresql in your system, one of the way is brew install postgresql (if you use homebrew- recommended) or download the postgres app from postgresapp.com, pg_config should come with postgres and psycopg2 is trying to find it.

104
votes

I ran pip install psycopg2-binary and it worked like charm

More info about the binary package

51
votes

To install psycopg2 you need have installed server before( I have installed PostgresApp)

Run manually command including the path of pg_config program in PATH env variable, in my case:

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

and then run

pip3 install psycopg2

EDIT:

Check directory of pg_config:

which pg_config
11
votes

OSX doesn't include PostgreSQL anymore, so you need to install it in some way to build the binary part of psycopg2 module.

I've used both brew and port. Installing any PostgreSQL version through one of them will enable you to build the module.

If you install Postgres in other ways, you need to check that the executable pg_config in in your path.

You can check for pg_config presence using the command

which -a pg_config

If you have Postgres installed and the aforementioned command doesn't return anything, you need to manually find the pg_config executable and put its containing directory in your PATH with:

export PATH=/path/to/postgresql/bin/:$PATH

Edit:

If you already installed it through homebrew, but it is not in your path, you should check if the /usr/local/bin directory is present in your path and add it if missing

If the directory is there, you can try to relink postgres with the following command

brew unlink postgresql && brew link postgresql
10
votes

My issue was that for some reason from within VENV pip install psycopg2 does not work but I found that running this command from inside of VENV

env LDFLAGS='-L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib' pip install psycopg2

allow me to install it on mac.

7
votes

If you use Docker and you do not want to install postgresql in you host system, you can try to use package psycopg2-binary and use postgresql in container.

5
votes

I was fighting with this issue for a complete day. Then I ran a script that I found in Pillow library Issues and solve the problem. Check it out https://github.com/python-pillow/Pillow/issues/3438#issuecomment-435169249

4
votes

On my MAC, this did the trick:

pip install psycopg2-binary
3
votes

Even I was unable to install psycopg2 on MacOx 10.15.6.

I tried pip install psycopg2-binary and it worked.

2
votes

$ find / -name pg_config 2>/dev/null

/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

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

$ pip install psycopg2

1
votes

The problem of psycopg2 installation and its import in the .py file can lie in two areas - installation and import.

  1. If your psycopg2 is not getting installed or giving an installation error then check you may have a problem with PostgreSQL installation, linking to pg_config file in the bin folder of PostgreSQL installation or openssl installation and its linkages.
  2. If psycopg2 is getting installed but you are unable to import it in your .py file then the problem is libpq and its linkages. The overall steps are reproduced below. You can check it step by step to know which is the source of error for you and then you can troubleshoot from there.

    Following are the steps, which worked for me and my team members while installing psycopg2 on Mac OS Big Sur. Before starting make sure you have the Xcode command-line tool installed. If not, then install it from the Apple Developer site. The below steps assume you have homebrew installed. If you have not installed homebrew then install it. Last but not the least, it also assumes you already have PostgreSQL installed in your system, if not then install it. Different people have different preferences but the default installation method on the official PostgreSQL site via Enterprise DB installer is the best method for the majority of people.
  • Put up the linkage to pg_config file in your .zshrc file by:
    export PATH="$PATH:/Library/PostgreSQL/12/bin:$PATH". This way you are having linkage with the pg_config file in the /Library/PostgreSQL/12/bin folder. So if your PostgreSQL installation is via other means, like Postgres.app or Postgres installation via homebrew, then you need to have in your .zshrc file the link to pg_config file from the bin folder of that PostgreSQL installation as psycopg2 relies on that.

  • Install OpenSSL via Homebrew using the command brew install openssl. The reason for this is that libpq, the library which is the basis of psycopg2, uses openssl - psycopg2 doesn't use it directly. After installing put the following commands in your .zshrc file:

    • export PATH="/usr/local/opt/[email protected]/bin:$PATH"
    • export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
    • export CPPFLAGS="-I/usr/local/opt/[email protected]/include"
      By doing this you are creating necessary linkages in your directory. These commands are suggested by brew while you install openssl and have been directly picked up from there.
  • Now comes the most important step, which is to install libpq using the command brew install libpq. This installs libpq library. As per the documentation

libpq is the C application programmer's interface to PostgreSQL. libpq is a set of library functions that allow client programs to pass queries to the PostgreSQL backend server and to receive the results of these queries.

  • Link libpq using brew link libpq, if this doesn't work then use the command: brew link libpq --force.
  • Also put in your .zshrc file the following
    export PATH="/usr/local/opt/libpq/bin:$PATH". This creates all the necessary linkages for libpq library .
  • Now restart the terminal or use the following command source ~/.zshrc.

  • Now use the command pip install psycopg2. It will work. This works, even when you are working in conda environment.

    N.B. pip install psycopg2-binaryshould be avoided because as per the developers of the psycopg2 library

The use of the -binary packages in production is discouraged because in the past they proved unreliable in multithread environments. This might have been fixed in more recent versions but I have never managed to reproduce the failure.

0
votes

I've had issues with this. What I did was make sure that Python was up to date with how I was pip installing. It required a "pip3" install.

0
votes

To install psycopg2 in mac, follow the below steps

  1. Goto Application folder
  2. Select Postgre.app
  3. Right click and "Show package contents"
  4. Select Content-->Versions-->{Version Number}-->bin
  5. Check the version number

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/{Version Number}/bin/

Example

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

Then run the below

pip3 install psycopg2

This should fix your issue

0
votes

having same issue on big sur, in my case i already install postgresql. Somehow i restart the machine and retry to install psycopg2 and it just work fine.

0
votes

Install OpenSSL and run: export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/. This command exports its path.