How to install psycopg2 using Pip?

10,944 views
Skip to first unread message

Andre Lopes

unread,
Mar 24, 2011, 11:01:43 AM3/24/11
to django...@googlegroups.com
Hi,

This question is not directly related with Django, but with Python.

I have installed "virtualenv" to have a virtual environment. Now I
need to instal "psycopg2" in my virtual environment, but I have not
successfully installed.

My steps:

[quote]
pip install
http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
[/quote]

And I got this message with an error:

[quote]
Downloading/unpacking
http://pypi.python.org/packages/source/p/psycopg2/psycopg2
-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
Downloading psycopg2-2.4.tar.gz (607Kb): 607Kb downloaded
Running setup.py egg_info for package from
http://pypi.python.org/packages/sou
rce/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
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

creating pip-egg-info\psycopg2.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

writing manifest file 'pip-egg-info\psycopg2.egg-info\SOURCES.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
Storing complete log in C:\Documents and Settings\anlopes\Application
Data\pip\p
ip.log
[/quote]

My question:

How can I tell to "pip" where is my pg_config?

Best Regards,

Shawn Milochik

unread,
Mar 24, 2011, 11:10:02 AM3/24/11
to django...@googlegroups.com

Ensure that pg_config is on your PATH and it should work.

Andre Lopes

unread,
Mar 24, 2011, 11:36:47 AM3/24/11
to Django users
Thanks for the reply.

How can I do that? I'm new to Python, I'm not aware of what is Python
path.

Best Regards,

Jacob Kaplan-Moss

unread,
Mar 24, 2011, 11:44:53 AM3/24/11
to django...@googlegroups.com
Hi Andre --

This indicates that you don't have the development packages for
PostgreSQL. psycopg2 is written mostly in C, and needs to be compiled
and linked against PostgreSQL. It does this by running a command
called pg_config, which is part of the PostgreSQL dev package.

You haven't indicated which platform you're on, so I can't tell you
exactly how to install the dev packages. On Ubuntu, it'd be "apt-get
install libpq-dev", for example.

Since this isn't really Django-related, you might want to also take a
look at the psycopg2 docs, especially the parts about building
problems: http://www.initd.org/psycopg/docs/faq.html#problems-compiling-and-deploying-psycopg2.

Good luck,

Jacob

Shawn Milochik

unread,
Mar 24, 2011, 11:49:05 AM3/24/11
to django...@googlegroups.com
This actually has nothing to do with Python -- I'm talking about your
PATH, not your PYTHONPATH.

Short version: Type 'which pg_config' at the command line. If it is
found, then it's on your PATH. Otherwise, it's not, which appears to
be the case.

If you want to just do a temporary fix, you can follow these steps:

Run this:
find / -name pg_config 2>>/dev/null

#let's assume the result of the previous command is this:
/usr/lib/postgresql/8.4/bin/pg_config

Add that path to your PATH temporarily

export PATH=$PATH:/usr/lib/postgresql/8.4/bin

You should then be able to do the pip install.

If that doesn't work because pg_config isn't found on your system at
all then see Jacob's suggestions.

Shawn

Andre Lopes

unread,
Mar 24, 2011, 12:03:30 PM3/24/11
to Django users
I have solved.

I just edited "setup.cfg" on the psycopg2 and added:

pg_config=C:\Program Files\PostgreSQL\8.4\bin\pg_config.exe

Thanks for the help.

Best Regards,

Shawn Milochik

unread,
Mar 24, 2011, 12:18:23 PM3/24/11
to django...@googlegroups.com
On Thu, Mar 24, 2011 at 12:03 PM, Andre Lopes <lopes8...@gmail.com> wrote:
> I have solved.
>
> I just edited "setup.cfg" on the psycopg2 and added:
>
> pg_config=C:\Program Files\PostgreSQL\8.4\bin\pg_config.exe


Yeah, that's the easiest method, but you specifically wanted to know
how to install it with pip. That way you have to install the module
manually. But it works. ;o)

Shawn

Mehrdad Majzoobi

unread,
Jul 23, 2012, 3:18:02 PM7/23/12
to django...@googlegroups.com
Ok, I realized what the issue was. In the setup.cfg file, the pg_config path had to include the pg_config executable name as well:

pg_config = /usr/pgsql-9.1/bin/pg_config

On Monday, July 23, 2012 2:01:29 PM UTC-5, Mehrdad Majzoobi wrote:
Guys, I have a similar problem but the difference is pg_config is actually in my PATH:

$ which pg_config
/usr/pgsql-9.1/bin/pg_config

I get the following error when I try to pip install psychopg2

###################

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 /tmp/pip-build/psycopg2

###################

I tried adding pg_config path to the setup.cfg file and build it using the source files I downloaded from their website and I get the following error message!

Error: Unable to find 'pg_config' file in '/usr/pgsql-9.1/bin/'


But it is actually THERE!!!

I am baffled by these errors. Can anyone help please?

By the way, I sudo all the commands. Also I am RHEL 5.5.
Message has been deleted

Fabio Caritas Barrionuevo da Luz

unread,
Jun 4, 2014, 9:33:47 AM6/4/14
to django...@googlegroups.com

If Ubuntu 12.04 or higher

do:

sudo apt-get install libpq-dev

Dow Street

unread,
Jun 4, 2014, 10:52:35 AM6/4/14
to django...@googlegroups.com
My understanding is that psycopg2 looks for some postgres files during the install. If you have postgres already installed on the same box, you should be able to resolve this problem by adding the postgres folder to your PATH environment variable before running 'pip install psycopg2'. The syntax for modifying the PATH might differ a little based on the operating system, but it would normally be something like this:

PATH=$PATH:/Library/PostgreSQL/9.1/bin

Here postgres is installed at /Library/PostgreSQL/9.1/bin, but it might be in a different location on your machine. As Fabio mentioned, if you do not have postgres installed on the same machine you may be able to add the development libraries for your OS and get it to work.

One note about modifying the PATH - due to the way virtualenv can adjust PATH contents you may need to add the postgres folder to the PATH after the virtualenv is activated. (not sure about this ...)

Hope that helps!
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/772ac1dc-e7c2-415c-bc6e-21a1e5b0ec4c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ilya Kazakevich

unread,
Jun 4, 2014, 2:21:39 PM6/4/14
to django...@googlegroups.com
Hello,

To be installed, psycopg2 needs to know where PostgreSQL headers (.h files)
and libraries (.a files) are situated. Header files are required by compiler
while libraries are required by linker.

PostgreSQL provides special tool named "pg_config" that tells everyone where
PostgreSQL is installed and where one should look for that files.
This tool is part of postgres installation.

Check you can run this program from your terminal. If you can't -- you need
to find it and add it's folder to your PATH variable.

"pip install psycopg2" runs "pg_config" to find your postgres installation.

Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"
>https://groups.google.com/d/msgid/django-users/44FC535C-1446-4673-8662-90
>6D54E3C247%40prodigy.net.
Message has been deleted
Message has been deleted

Sugatang Itlog

unread,
Jun 12, 2014, 6:26:46 AM6/12/14
to django...@googlegroups.com
Hi - When installing psycopg2 and errors like ...

Error: pg_config executable not found

do the following ....

[root@localhost ~]# which -a pg_config
/usr/bin/which: no pg_config in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

[root@localhost ~]# find / -iname 'pg_config' 2>/dev/null
/usr/pgsql-9.3/bin/pg_config

[root@localhost ~]# ln -s /usr/pgsql-9.3/bin/pg_config /usr/bin/pg_config

Then install psycopg2 via pip. If still got errors install the postgresql devel then do the above steps again.

Thanks.
SGTItlog

Bill Freeman

unread,
Jun 16, 2014, 12:35:38 PM6/16/14
to django-users
One possibility is that you have it, but it is in a directory that is not on you path. Try:

   find / -name pg_config 2> /dev/null

If this finds the executable, you can add the directory your current invocation of the shell (It will be gone when you log out and back in) to do the pip install.

But you may not find it, in which case you need to install the PostgreSQL "development" package (xxx-dev on deb based systems, xxx-devel on .rpm based systems, for other systems you will need to do your own research).  Having installed it, you will still probably have to do the steps above.  All this can be found using google.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Kat R

unread,
Nov 21, 2014, 5:30:20 PM11/21/14
to django...@googlegroups.com, sh...@milochik.com
This was helpful. Thank you. :)
Reply all
Reply to author
Forward
0 new messages