Mac + virtualenv + pip + postgresql =错误:找不到pg_config可执行文件

我试图安装一个教程的postgres,但pip给我错误:

 pip install psycopg 

我得到一个错误的片段:

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

在我的virtualenv pg_config在哪里? 如何configuration? 我使用的是virtualenv,因为我不想在系统范围内安装postgres。

在Mac上,如果您使用的是Postgres.app ,则pg_config文件位于/Applications/Postgres.app/Contents/Versions/<current_version>/bin目录中。 这将需要被添加到您的系统path来解决这个错误,如下所示:

 export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/<current_version>/bin 

所以例如,如果当前的Postgres.app版本是9.5,那么这个输出行将是:

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

在Mac上,解决scheme是安装postgresql

 brew install postgresql 

在CentOS上,解决scheme是安装postgresql-devel

 sudo yum install postgresql-devel 

pg_configpostgresql-devel包中

我完全同意john hight的观点,大部分的答案完全是offtopic,假设OP精确地指定了需要使用virtualenv

对我来说,答案是在启动virtualenv的同时运行以下命令:

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

(请注意,第9.4部分代表版本,可能会有所不同),然后:

 pip install psycopg2 

假设你已经安装了postgres。 如果没有,那么请记住,最好的和推荐的解决scheme是使用: Postgres.app

不要忘了你的$ PATHvariables在虚拟环境中!=你的全局$ PATHvariables。 您可以在virtualenv中使用'echo $ PATH'来确认,也可以在新的shell中进行确认。 所以,除非你想在你的虚拟环境中安装PostgreSQL作为一个独特的实例(不是一件值得做的事情,imo),你需要修改virtualenv中的$ PATHvariables来包含你的全局安装的path解决你丢失的pg_config错误)。

这里是步骤:

1.)在新的shell中,input“which pg_config”。 这将返回path。 复制它。 就我而言,path如下所示:/Applications/Postgres.app/Contents/Versions/9.3/bin

2.)回到你的virtualenv shell中,input“export PATH = /你的path到pg_config:$ PATH”

3.)然后,仍然在virtualenv,“pip安装psycopg2”

如果一切按计划进行,这将在虚拟环境中安装psycopg2,但安装将引用您的Global PostgreSQL安装。 在我的情况下,这个全局安装通过Postgres.App安装,因此是path。 我更喜欢这种使用psycopg2的方法,因为这意味着我可以在任何虚拟环境中轻松使用数据库,而不是仅在定义的虚拟环境中使用。

希望这有助于任何人到达这里。 对于Google juice,当遇到这个问题时,会返回显式(和模糊的)错误语言:
命令python setup.py egg_info失败,错误代码1

以下是我能够在我的Mac(OSX 10.9)上解决这个问题的方法:

 brew update brew install --force ossp-uuid brew install postgresql pip install psycopg 

当我尝试pip install psycopg ( LLVM 5.1问题 )时,我得到了一个CLANG错误,所以我不得不使用这个命令安装psycopg:

 ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install psycopg 

这与Mingyu的解决scheme类似,但是我认为这是值得分享的。

当构build工具找不到Postgresql库时,会导致此错误。

通常需要指示psycopg2如何findpg_config二进制文件,您可以:

  1. 在你的shellpath(/ usr / local / pgsql / bin /)中添加pg_config的path

  2. 或编辑psycopg2源文件夹中的setup.cfg文件,并在以pg_config开头的行上提供pg_config的完整path=

pg_config =在/ usr /本地/ pgsql的/ bin中/ pg_config

  • 上面是一个例子,你可以locate pg_configfind它所在的位置,或者只需键入which pg_config ,它应该告诉你path。

错误来自没有在系统上安装postgresql的错误。 如果是这样,请下载并构buildpostgres,或者下载OS X的预构build的psycopg2二进制文件。

virtualenv是用于python包的。 我不认为你可以在virtualenv中包含postgres。 你看到的错误信息大概是因为你还没有安装postgres。 psycopg2安装脚本正在寻找postgres文件(在这种情况下是pg_config),而不是find它们,因为它没有安装。 postgres不能使用pip或virtualenv进行安装。

除了@bkev和@andi提供的答案之外,根据Postgres.app上的文档 ,您应该将以下内容添加到Mac上的bash_profile:

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

请注意,没有硬编码的版本号。 我想添加这个作为上述答案的评论,但我没有足够的代表这个。

对于OS X El Captain (10.11.6) + brew + virtualenv + PostgreSQL 9.5

安装PostgreSQL 9.5

 brew install postgresql@9.5 

打开你的terminal并执行:

 export PATH=$PATH:/usr/local/opt/postgresql\@9.5/bin/ pip install psycopg2 

Ubuntu上,我只需要postgres开发包:

 sudo apt-get install postgresql-server-dev-all 

如果您使用的是postgresql 9.4,则该文件位于

 /usr/pgsql-9.4/bin/pg_config 

包的名字是

 postgresql94-9.4.9-1PGDG.rhel6.x86_64 

要将pg_config添加到PATH,请执行以下操作

 PATH=$PATH:/usr/pgsql-9.4/bin/