Error Installing Psycopg2 on MacOS 10.9.5
Asked Answered
S

27

102

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
Spec answered 23/11, 2015 at 8:42 Comment(1)
U
162

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

More info about the binary package

Python 3

pip3 install psycopg2-binary
Uncommitted answered 5/10, 2019 at 10:11 Comment(5)
Yay. I was getting errors with just pip install psycopg2 but using psycopg2-binary works.Persephone
I don't think this really answers the question asked. Note that, according to the package description, "The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources." The homepage goes further and explains that the binary version may cause segfaults due to bundling its own libssl binary.Erumpent
This was the good answer for me, but i had to fix "library not found for -lssl", here is how i fixed it : $ export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include" and then $ pip install psycopg2-binaryPaquette
pip install psycopg2-binary I get this message. Requirement already satisfied: psycopg2-binary in ./env/lib/python3.8/site-packages (2.9.3) But the error remains.Frye
I used the exports that @Paquette supplied, but then ran $ pip install psycopg2Kulturkampf
A
116

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.

Acetylide answered 23/11, 2015 at 8:51 Comment(4)
Warning: postgresql-9.4.5_2 already installed, it's just not linked gives the terminalSpec
how you notice not have installed postgres was the problem, i cant read anything related on the error messageRemise
pg_config executable this line usually means that postgres installation that python was linking to isn't found.Acetylide
To run directly from a Python interpreter on my Mac, this is what I needed to do.Sorilda
H
60

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
Hinkle answered 5/3, 2016 at 17:44 Comment(2)
works for me if I remove the @ in @latest (with pip install psycopg2-binary)Magnetize
That did the trick for me, thanks!Mottle
U
15

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
Univalent answered 23/11, 2015 at 8:54 Comment(9)
Thanks for the help. I have installed postrgresql and psycopg2 with my terminal. When I try to add the psychopg2 to my pycharm interpreter i still get the error..Spec
You should check which interpreter you are using in pycharm. If you have more than one version of python installed, you need to configure pycharm to pick the right one. You can do it in preferences, by searching interpreter in the search box and editing Project: $projectName -> Project InterpreterUnivalent
yes, i know. The only thing I need to know now is how I get psycopg2 installed via pip in a specific version of python. For python 2.5 it was succeed. But i need to install it for python 3.4. I'm using google to find a solution..Spec
if you know that pip is installed in the target python version, you can simply execute: /path/to/3.4/python /usr/bin/pip install psycopg2 if not, you can follow the pip installation instruction running the get-pip.py with the desired version.Univalent
thx for your help mnecia. I get the error "can't open file 'usr/bin/pip'"Spec
It looks like you miss a '/' in front of the pip path. You can check the pip executable path with which pipUnivalent
Let us continue this discussion in chat.Univalent
The brew unlink and brew link returned exactly the export command I had to run. After that just . ~/.bash_profileTachistoscope
Thank you, I just had to export my path to export PATH=/Library/PostgreSQL/13/bin:$PATH and psycopg2 installed successfully.Collective
B
11

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.

Bunnell answered 24/9, 2020 at 15:32 Comment(1)
In my case openssl was at /usr/local/opt/[email protected]. When I used with corrected path it worked. export LDFLAGS="-L/usr/local/opt/[email protected]/lib" export CPPFLAGS="-I/usr/local/opt/[email protected]/include"Schooling
E
10

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.

Equable answered 9/1, 2019 at 13:7 Comment(0)
M
9

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.

Mandate answered 28/5, 2021 at 12:7 Comment(1)
When using the alternative postgresql installer from: postgresapp.com, this approach worked with the following modifications to my .zshrc file: export PATH="$PATH:/Applications/Postgres.app/Contents/Versions/14/bin:$PATH" export LDFLAGS="-L/Applications/Postgres.app/Contents/Versions/14/lib" export CPPFLAGS="-I/Applications/Postgres.app/Contents/Versions/14/include"Abie
N
6
$ 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
Now answered 24/11, 2019 at 6:13 Comment(3)
find / -name pg_config 2>/dev/null is worked for me, thanksRama
Thank you it helps me to avoid spending more times on other pages.Voltcoulomb
this answer is so underrated... this is the actual solution to the listed problem.. thanks for the clear and concise solution..Akins
A
5

On my MAC, this did the trick:

pip install psycopg2-binary
Anticipatory answered 22/5, 2020 at 5:26 Comment(2)
pyscopg2-binary is just for development and should not be used in production buildCuttlefish
pip install psycopg2-binary I get this message. Requirement already satisfied: psycopg2-binary in ./env/lib/python3.8/site-packages (2.9.3) But the error remains.Frye
C
5

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.

Chortle answered 4/5, 2022 at 11:9 Comment(0)
B
4

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

Blackandwhite answered 14/5, 2019 at 22:29 Comment(4)
Could you please add some relevant piece of solution aside from the link? It would make your answer more descriptive and self-sufficient.Blower
How is the error mentioned in the linked github issue similar to the error mentioned by OP? I'm looking at the github issue and I don't see the connection.Trainee
I really don't know the connection and why the sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target / script works. I just was having the same problem with the psycopg2 installation. If the solution fit to someone I'll glad to help. If I breaking a rule or policy let me know and I'll erase the post.Nod
No explanation and broken link...Dazzle
R
4

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.

Reinaldoreinaldos answered 20/2, 2022 at 14:49 Comment(0)
S
3

Even I was unable to install psycopg2 on MacOx 10.15.6.

I tried pip install psycopg2-binary and it worked.

Shaffer answered 15/9, 2020 at 8:49 Comment(0)
E
2

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

Erna answered 16/3, 2021 at 18:20 Comment(2)
I had the error ' ld: library not found for -lssl' and this worked.Kermanshah
I installed openssl with homebrew i.e. brew install openssl. then I needed to run export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib" which I got from running brew info openssl. then pip3 install psycopg2-binary --user workedOverbold
E
2

First run brew install postgresql, then run pip install psycopg2

Exocrine answered 21/4, 2022 at 20:17 Comment(0)
B
1

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

Bumbledom answered 21/8, 2020 at 12:8 Comment(0)
P
1

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
Passepartout answered 15/9, 2021 at 15:46 Comment(0)
C
0

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.

Chromatogram answered 23/6, 2020 at 19:37 Comment(0)
H
0

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.

Hemia answered 12/3, 2021 at 7:9 Comment(0)
S
0

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 ;)

Shakira answered 8/1, 2022 at 10:43 Comment(0)
R
0

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

Rapparee answered 12/1, 2022 at 13:23 Comment(0)
F
0

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
Frye answered 21/4, 2022 at 9:58 Comment(0)
D
0
  1. PATH=$PATH:/Library/PostgreSQL/13/bin #the path is where your postgresql is installed. Usually inside Macintosh HD - lIBRARY - POSTGRESSQL/INSTALLED VERSION/BIN
  2. pip3 install --upgrade wheel
  3. pip3 install --upgrade setuptools
  4. pip3 install psycopg2

I got these steps from "https://www.youtube.com/watch?v=N4RxnQH2pVY"

Douglass answered 16/7, 2022 at 23:11 Comment(0)
P
0
% export PG_HOME=/Library/PostgreSQL/14

% export PATH=$PATH:$PG_HOME/bin 

% pip install psycopg2

this will work. Thanks

Pindus answered 18/8, 2022 at 15:10 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Vacuole
T
0

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

Truly answered 30/6, 2023 at 12:18 Comment(0)
T
-1
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.

Turkoman answered 29/11, 2021 at 22:0 Comment(1)
Actually... That didnt work. I had to go to postgresapp.com, download postgres. from there, uninstall all psycopg from my python list. Then, run postgres. Run the command -> psql -U postgres <- in the terminal. Then i ran the command -> sudo mkdir -p /ect/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /ect/paths.d/postgresapp <- in my python IDE terminal. After all of this, I was able to do -> pip install psycopg2-binary <- in my python terminal and its now running.Turkoman
R
-1

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.

Rearmost answered 4/12, 2021 at 10:33 Comment(1)
Not work's for me. I'm using macOs catalina (version 10.15.7).Frye

© 2022 - 2024 — McMap. All rights reserved.