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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I ran pip install psycopg2-binary and it worked like charm
More info about the binary package
Python 3
pip3 install psycopg2-binary
Method 2
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.
Method 3
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
Method 4
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
Method 5
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.
Method 6
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.
Method 7
The problem of psycopg2 installation and its import in the .py file can lie in two areas – installation and import.
- If your
psycopg2is not getting installed or giving an installation error then check you may have a problem with PostgreSQL installation, linking topg_configfile in thebinfolder of PostgreSQL installation oropensslinstallation and its linkages. - If
psycopg2is getting installed but you are unable to import it in your.pyfile then the problem islibpqand 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_configfile in your.zshrcfile by:
export PATH="$PATH:/Library/PostgreSQL/12/bin:$PATH". This way you are having linkage with thepg_configfile in the/Library/PostgreSQL/12/binfolder. So if your PostgreSQL installation is via other means, like Postgres.app or Postgres installation via homebrew, then you need to have in your.zshrcfile the link topg_configfile from thebinfolder of that PostgreSQL installation aspsycopg2relies on that. -
Install OpenSSL via Homebrew using the command
brew install openssl. The reason for this is thatlibpq, the library which is the basis ofpsycopg2, uses openssl –psycopg2doesn’t use it directly. After installing put the following commands in your.zshrcfile: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
libpqusing the commandbrew 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
.zshrcfile the following
export PATH="/usr/local/opt/libpq/bin:$PATH". This creates all the necessary linkages forlibpqlibrary . - 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.
Method 8
On my MAC, this did the trick:
pip install psycopg2-binary
Method 9
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
Method 10
$ 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
Method 11
There is no PostgreSQL database installed on a mac computer, and psycopg2 is looking for Postgres files. Simply install the Postgres database and re-run pip install it will work.
https://postgresapp.com/downloads.html
From the above link download Postgres.
Drag and drop in your application folder.
Initialize the database last step is to configure your path.
sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
This will ask for your mac password enter it.
You can copy-paste the above link in the terminal and press enter.
For more information about where this link comes from check the below link:
https://postgresapp.com/
Restart your terminal
to test write the following command:
psql -U postgres
the command will prompt you to Postgres terminal.
Once all of this is done try to reinstall psycopg-2 it will work.
Method 12
Even I was unable to install psycopg2 on MacOx 10.15.6.
I tried pip install psycopg2-binary and it worked.
Method 13
To install psycopg2 in mac, follow the below steps
- Goto Application folder
- Select Postgre.app
- Right click and “Show package contents”
- Select Content–>Versions–>{Version Number}–>bin
- 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
Method 14
Install OpenSSL and run: export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/. This command exports its path.
Method 15
If you’ve already installed PostgreSQL from the official installer and still get this error during psycopg2 installation via pip in the virtual environment created via venv, follow these steps:
- open Terminal (this is outside the virtual environment), find pg_config’s location with the command:
sudo find / -name pg_config - once you find the location from the list of output, add to the PATH from inside the virtual environment:
export PATH={location}:$PATH(make sure you replace the ‘{location}’ with the folder you discovered) - then proceed to install psycopg2 in the virtual environment:
pip install psycopg2
Method 16
First run brew install postgresql,
then run pip install psycopg2
Method 17
TLDR:
brew install postgresql pip install psycopg2
Why this happens to you?
This issue happens mainly in MacBooks with Apple Chip (M1)
A way to solve it is install Postgres in your Mac using brew. psycopg2 is a library related to Postgres so, installing a complete version of Postgres include the installation of the missing libraries.
Try
brew install postgresql
this will install a Postgres server locally in your MacBook.
If this gives you an error it may be because you do not have installed brew, I strongly recommend to all Mac users to install it.
To check that you have brew installed:
brew -v
If this not work please install brew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After the installation of postgresql with brew install the library again with pip as you was trying:
pip install psycopg2
Probably this issue will be solved in future and the default pip installation will work when the support for Apple chip increase.
Method 18
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.
Method 19
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.
Method 20
pip install psycopg binary
This worked for me when i installed in the terminal. It took me about 45 mins to figure out not to use any – or [] like every other documentation i saw.
Method 21
Since I stumbled upon this thread having trouble installing psycopg2 on macOS 11.2, the solutions did not work for me anymore, since the pathes for Homebrew have changed.
For me, that worked instead:
$ env LDFLAGS="-I/opt/homebrew/Cellar/[email protected]/1.1.1m/include -L/opt/homebrew/Cellar/[email protected]/1.1.1m/lib" pip install psycopg2
I would assume that in the future those paths may have changed again 😉
Method 22
If you are trying to install psycopg2 inside an activated conda environment and you are getting this error, run conda install psycopg2 instead of pip install psycopg2
Worked for me on Monterey 12.1
Method 23
In my case as the error detail was :
#error architecture not supported
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
I tell pip what architecture I’m using and now psycopg2 can install without trouble.
ARCHFLAGS="-arch x86_64" pip3 install nltk
Method 24
- PATH=$PATH:/Library/PostgreSQL/13/bin
#the path is where your postgresql is installed. Usually inside Macintosh HD – lIBRARY – POSTGRESSQL/INSTALLED VERSION/BIN - pip3 install –upgrade wheel
- pip3 install –upgrade setuptools
- pip3 install psycopg2
I got these steps from “https://www.youtube.com/watch?v=N4RxnQH2pVY”
Method 25
For those who have installed PostgressApp but still getting the same error:
You need to follow the 3rd step in the setup as described in the setup docs: https://postgresapp.com/
Run these two commands to set the path of your PostgressApp
sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
Remember to restart ALL your terminal windows for changes to take place.
After that run as usual
pip install psycopg2
This time it should work as expected.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0