Mac + virtualenv + pip + postgresql = Error: pg_co

2019-01-12 21:29发布

I was trying to install postgres for a tutorial, but pip gives me error:

pip install psycopg

A snip of error I get:

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

Where is pg_config in my virtualenv? How to configure it? I'm using virtualenv because I do not want a system-wide installation of postgres.

14条回答
可以哭但决不认输i
2楼-- · 2019-01-12 22:16

In addition to the answers provided by @bkev and @andi, according to the documentation on Postgres.app, you should add the following to your bash_profile on Mac:

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

Note that, there is no hard-coded version number. I wanted to add this as a comment to above answers, but I don't have enough rep for this.

查看更多
爷、活的狠高调
3楼-- · 2019-01-12 22:18

If you don't have to use the psycopg driver specifically, switch to the pg8000 driver. It's pure Python and less finicky.

查看更多
男人必须洒脱
4楼-- · 2019-01-12 22:19

On Mac, the solution is to install postgresql:

brew install postgresql

On CentOS, the solution is to install postgresql-devel:

sudo yum install postgresql-devel

pg_config is in postgresql-devel package

查看更多
Ridiculous、
5楼-- · 2019-01-12 22:19

Mine was located in /Library/PostgreSQL/9.4/bin

export PATH=$PATH:/Library/PostgreSQL/9.4/bin
查看更多
老娘就宠你
6楼-- · 2019-01-12 22:20

This error is caused when the build tools can't find the Postgresql libraries.

Often it's required to instruct psycopg2 how to find the pg_config binary, you can:

  1. add the path to pg_config in your shell path (/usr/local/pgsql/bin/)

  2. or edit the setup.cfg file in the psycopg2 source folder and provide the full path to pg_config on the line that starts with pg_config=

pg_config=/usr/local/pgsql/bin/pg_config

  • the above is an example, you can do locate pg_config to find out where it resides, or simply type which pg_config and it should tell you the path.

Less often the error comes from not having postgresql installed on your system. If so, download and build postgres, or download a pre-built psycopg2 binary for OS X.

查看更多
Lonely孤独者°
7楼-- · 2019-01-12 22:26

For OS X El Captain (10.11.6) + brew + virtualenv + PostgreSQL 9.5:

After installing PostgreSQL 9.5:

brew install postgresql@9.5

Open your terminal and execute:

export PATH=$PATH:/usr/local/opt/postgresql\@9.5/bin/
pip install psycopg2
查看更多
登录 后发表回答