Reputation: 2394
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
Upvotes: 107
Views: 196811
Reputation: 1
I will try to summarize all answers.
I solved the problems and installed the psycopg2 on my macos Ventura 13.4.
Not via psycopg2-binary! Note: dont use psycopg2-binary in production, because:
"The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources."
Follow these steps:
Install postgresql if you haven't already:
brew install postgresql
Check the pg_config program: you have this file if you installed postgres, which installed libpq-dev:
pg_config --version
if it returns an error or an unexpected version number, find the pg_config file:
find /usr/ -name pg_config
and do:
export PATH=<path>:$PATH
for example:
export PATH=/usr/local//Cellar/postgresql@14/14.8_2/bin/:$PATH
If the error raised again, check traceback and if you see some text like this:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Then run
xcode-select --install
These steps worked for me.
Official build prerequisites: https://www.psycopg.org/docs/install.html#build-prerequisites
Upvotes: 0
Reputation: 1
% export PG_HOME=/Library/PostgreSQL/14
% export PATH=$PATH:$PG_HOME/bin
% pip install psycopg2
this will work. Thanks
Upvotes: 0
Reputation: 176
I got these steps from "https://www.youtube.com/watch?v=N4RxnQH2pVY"
Upvotes: 0
Reputation: 2502
I ran pip install psycopg2-binary
and it worked like charm
More info about the binary package
Python 3
pip3 install psycopg2-binary
Upvotes: 169
Reputation: 664
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.
Upvotes: 6
Reputation: 378
First run brew install postgresql
,
then run pip install psycopg2
Upvotes: 2
Reputation: 4093
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
Upvotes: 0
Reputation: 41
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.
Upvotes: 4
Reputation: 81
$ 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
Upvotes: 5
Reputation: 780
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
Upvotes: 0
Reputation: 1
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/openssl@1.1/1.1.1m/include -L/opt/homebrew/Cellar/openssl@1.1/1.1.1m/lib" pip install psycopg2
I would assume that in the future those paths may have changed again ;)
Upvotes: 0
Reputation: 119
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.
Upvotes: -1
Reputation: 59
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.
Upvotes: -1
Reputation: 11
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:
sudo find / -name pg_config
export PATH={location}:$PATH
(make sure you replace the '{location}' with the folder you discovered)pip install psycopg2
Upvotes: 1
Reputation: 271
The problem of psycopg2
installation and its import in the .py
file can lie in two areas - installation and import.
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.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.
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/openssl@1.1/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
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.
brew link libpq
, if this doesn't work then use the command: brew link libpq --force
..zshrc
file the following export PATH="/usr/local/opt/libpq/bin:$PATH"
. This creates all the necessary linkages for libpq
library .source ~/.zshrc
. pip install psycopg2
. It will work.
This works, even when you are working in conda environment. pip install psycopg2-binary
should be avoided because as per the developers of the psycopg2 libraryThe 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.
Upvotes: 9
Reputation: 1500
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
Upvotes: 62
Reputation: 41
Install OpenSSL and run: export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
. This command exports its path.
Upvotes: 2
Reputation: 129
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.
Upvotes: 0
Reputation: 2299
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.
Upvotes: 11
Reputation: 31
Even I was unable to install psycopg2 on MacOx 10.15.6.
I tried pip install psycopg2-binary and it worked.
Upvotes: 3
Reputation: 2825
To install psycopg2 in mac, follow the below steps
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
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 2077
On my MAC, this did the trick:
pip install psycopg2-binary
Upvotes: 5
Reputation: 49
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
Upvotes: 4
Reputation: 529
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.
Upvotes: 11
Reputation: 3378
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
Upvotes: 15
Reputation: 12122
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.
Upvotes: 117