在 MacOS 10.9.5上安装 Psycopg2时出错

我正试图在我的 Macbook 上安装 Psycopg2,但是我收到了一个错误。我在 StackOverflow 上发现了很多相同的问题,但是没有答案。
我在用:

操作系统: MacOS 10.9.5
Python 版本: 3.4.3

我的错误代码是:

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
158122 次浏览

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.

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

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

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.

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

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

More info about the binary package

Python 3

pip3 install psycopg2-binary
$ 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

On my MAC, this did the trick:

pip install psycopg2-binary

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.

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

Even I was unable to install psycopg2 on MacOx 10.15.6.

I tried pip install psycopg2-binary and it worked.

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.

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.

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

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:
    .zshrc0. This way you are having linkage with the pg_config file in the .zshrc1 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"
      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.

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
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.

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.

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

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

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.

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

First run brew install postgresql, then run pip install psycopg2

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.

  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"

% export PG_HOME=/Library/PostgreSQL/14


% export PATH=$PATH:$PG_HOME/bin


% pip install psycopg2

this will work. Thanks