How to install psycopg2 with "pip" on Python?

511,744

Solution 1

Note: Since a while back, there are binary wheels for Windows in PyPI, so this should no longer be an issue for Windows users. Below are solutions for Linux, Mac users, since lots of them find this post through web searches.


Option 1

Install the psycopg2-binary PyPI package instead, it has Python wheels for Linux and Mac OS.

pip install psycopg2-binary

Option 2

Install the prerequsisites for building the psycopg2 package from source:

Debian/Ubuntu

Python 3

sudo apt install libpq-dev python3-dev

You might need to install python3.8-dev or similar for e.g. Python 3.8.

Python 2

sudo apt install libpq-dev python-dev

If that's not enough, try

sudo apt install build-essential

or

sudo apt install postgresql-server-dev-all

as well before installing psycopg2 again.

CentOS 6

See Banjer's answer

Solution 2

On CentOS, you need the postgres dev packages:

sudo yum install python-devel postgresql-devel

That was the solution on CentOS 6 at least.

Solution 3

On Mac Mavericks with Postgres.app version 9.3.2.0 RC2 I needed to use the following code after installing Postgres:

sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin pip install psycopg2

Solution 4

if you're on a mac you can use homebrew

brew install postgresql

And all other options are here: http://www.postgresql.org/download/macosx/

Good luck

Solution 5

I recently configured psycopg2 on a windows machine. The easiest install is using a windows executable binary. You can find it at http://stickpeople.com/projects/python/win-psycopg/.

To install the native binary in a virtual envrionment, use easy_install:

C:\virtualenv\Scripts\> activate.bat
(virtualenv) C:\virtualenv\Scripts\> easy_install psycopg2-2.5.win32-py2.7-pg9.2.4-release.exe
Share:
511,744
André
Author by

André

Updated on November 20, 2021

Comments

  • André
    André over 2 years

    I'm using virtualenv and I need to install "psycopg2".

    I have done the following:

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

    And I have the following messages:

    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
    

    My question, I only need to do this to get the psycopg2 working?

    python setup.py build_ext --pg-config /path/to/pg_config build ...
    
  • André
    André about 13 years
    Thanks for the reply. But I need to install to an virtual environment. Best Regard,
  • Praveen Gollakota
    Praveen Gollakota about 13 years
    This trick (courtesy, Ned Batchelder) has helped me in the past to install from binaries to a virtualenv
  • slacy
    slacy almost 13 years
    Is there really no way to get "pip install psycopg2" working on Windows? Even if a patch to psycopg2 is necessary, it would be worth it.
  • I159
    I159 over 12 years
    11.04: different errors, the same solution. Worship to dev versions!
  • joar
    joar over 12 years
    @I159 - The *-dev packages contain the files necessary to compile an application from source that uses the functions provided by the library (as psycopg2 uses the libpq and python libraries, among others).
  • Sam Joseph
    Sam Joseph about 12 years
    I got this error when postgres was not installed on my machine - I installed postgres and the error went away ...
  • xamox
    xamox about 12 years
    Works on Ubuntu 11.10 as well. Thanks!
  • caleb
    caleb almost 12 years
    I'm using Ubuntu 12.04 LTS, and I also had to sudo apt-get install postgresql-server-dev-all
  • Saher Ahwal
    Saher Ahwal almost 12 years
    are you sure that is right? I installed postgres on my windows machine and I still get that. ?
  • Saher Ahwal
    Saher Ahwal almost 12 years
    can you elaborate please. Where do you get the psycopg2 at the first place?
  • Saher Ahwal
    Saher Ahwal almost 12 years
    Why don't they have that on their site as pre-req to have postgres?
  • user2471801
    user2471801 almost 12 years
    windows binaries are available at stickpeople.com/projects/python/win-psycopg
  • Pravitha V
    Pravitha V almost 12 years
    how can i install libpq-dev python-dev packages in opensuse?
  • lajarre
    lajarre over 11 years
    Could someone please explain why not just the directory containing pg_config to the PATH (or use --pg-config)?
  • Seth
    Seth over 11 years
    Works for me on Ubuntu 12.10.
  • Duncan Parkes
    Duncan Parkes over 11 years
    I found I only needed to install libpq-dev, which is fortunate, as python-dev wouldn't install.
  • jpmc26
    jpmc26 almost 11 years
    This isn't just CentOS, I think. psycopg has native dependencies, so to build from source (which is what pip does), the PostgreSQL development libraries need to be there. (The C headers, maybe? I don't know much about compiling native code.)
  • David Berger
    David Berger over 10 years
    I'm missing something. I'm using virtualenv as a convenience to perform local installs because I don't have root privileges on my deployment server. Does anyone know the actual answer to the original question, because I do know that my machine and the deployment server do have the needed dependencies, I just won't have permission to re-deploy.
  • joar
    joar over 10 years
    @DavidBerger if you have the libpq-dev and python-dev packages installed, your virtualenv's pip should install it just fine.
  • David Berger
    David Berger over 10 years
    @joar Not entirely true. At least, I have the python modules I need in my site-packages, although I'm unsure how they got there. But pip crashes with the above error. Of course, my destination environment also has the necessary dependencies, so virtualenv --system-site-packages allows me to do what I need without pip install, but I didn't originally want to go that route.
  • Unknown Coder
    Unknown Coder about 10 years
    This was a lifesaver. Thanks a lot!
  • Ryder Brooks
    Ryder Brooks about 10 years
    I concur that this solves the problem on CentOS 6. I would like to add that I did have to add /usr/pgsql-9.3/bin to $PATH. Here is a post giving an example of how to do that. serverfault.com/questions/102932/…
  • user798719
    user798719 about 10 years
    sudo PATH=$PATH:/Applications/Postgres93.app/Contents/MacOS/bin pip install psycopg2 [as of April 5, 2014]
  • tbradley22
    tbradley22 almost 10 years
    nichochar's answer and attomos's answer (install postgresql and modify path) together resolved the issue for me. the easiest way to edit the path is to run "sudo nano /etc/path" from your terminal.
  • oden
    oden over 9 years
    Confirmed for Postgres.app 9.3.5 also :)
  • Richard D
    Richard D over 9 years
    With Postgres.app 9.3.5 Mac OS X Version 10.9.4, I had to add export PATH=${PATH}:/Applications/Postgres.app/Contents/Versions/9.‌​3/bin/ to the ~/.bash_profile and open a new window before psycopg2 installed. The path specified by @user798719 doesn't exist for me and I just installed Postgres.app on my computer today [September 11,2014].
  • elin3t
    elin3t over 9 years
    if you are using virtualenv you dont need 'sudo' so active your virtualenv and run 'PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3‌​/bin pip install psycopg2'
  • emecas
    emecas over 9 years
    My case OS X Ver 10.9.4: sudo PATH=$PATH:/Library/PostgreSQL/9.2/bin pip install psycopg2
  • BrockLee
    BrockLee over 9 years
    Thank you for this. I was about to go insane before I found this!
  • seanv507
    seanv507 over 9 years
    the stickpeople link also discusses virtual env and points to a pip install version github.com/nwcell/psycopg2-windows
  • iamfrancisyo
    iamfrancisyo over 9 years
    Yes, this worked for me too. After installing PostgreSQL, I had to set my path by doing PATH=$PATH:/Library/PostgreSQL/9.4/bin pip install psycopg2.
  • Lara
    Lara about 9 years
    And about Os x? I mean, is based on Unix but I think can be different.
  • Bret
    Bret about 9 years
    Any idea why this doesn't end up in the default path?
  • Dave
    Dave about 9 years
    I'm using a windows and inside a virtualenv and the easy_install psycopg2 worked like a charm.
  • Gordon Linoff
    Gordon Linoff almost 9 years
    As a note: this is an excellent example of why post-only answers should not be allowed. The link no longer works.
  • jayeshkv
    jayeshkv almost 9 years
    i tried this and i get psycopg2-2.6-cp34-none-win_amd64.whl is not a supported wheel on this platform.
  • qwerty
    qwerty almost 9 years
    Thanks, easy_install worked. Only problem was that it added to "venv\Lib\site-packages\psycopg2-2.6.1-py3.4-win32.egg\" directory this psycopg2 module/directory. All i had to do was copy it to site-packages folder
  • duozmo
    duozmo almost 9 years
    Why from source instead of Homebrew?
  • duozmo
    duozmo almost 9 years
    Worked for me too, no need to monkey with my path (and generally, I wouldn’t recommend sudo here - you should be able to do this in user space).
  • attomos
    attomos almost 9 years
    @duozmo it was answered in 2012 and I believe I did try to install it with Homebrew at that time but it failed. Now, I've installed PostgreSQL using Homebrew without any problems.
  • vijay
    vijay almost 9 years
    I got a error like that how to rectify thisplease help me!!! Command "/usr/bin/python -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-AUbwwG/psycopg2/se‌​tup.py';exec(compile‌​(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\n'), file, 'exec'))" install --record /tmp/pip-44Va05-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-build-AUbwwG/psycopg2
  • wswld
    wswld over 8 years
    It's not a good practice to mix pip packages and apt-get packages.
  • grim
    grim over 8 years
    Note that I didn't need to update the PATH environment variable.
  • radtek
    radtek over 8 years
    You shouldn't have to if you reload your terminal session.
  • derrend
    derrend over 8 years
    If you are here because you are having trouble installing psycopg2 into the python 3 Celery container from Dockerhub you will also need to install build-essential with sudo apt-get install -y build-essential
  • Diego Sarmiento
    Diego Sarmiento over 7 years
    Use the latest version: PATH=$PATH:/Applications/Postgres.app/Contents/Versions/late‌​st/bin pip install psycopg2
  • zelusp
    zelusp over 7 years
    Only @seanv507 comment solved my problem, however that linked repo is abandoned. Checkout this link for an updated solution (psycopg2.6.2 + Win64 + env)
  • Storm
    Storm over 7 years
    I had to do sudo apt-get install libpq-dev python3-dev as im using python3
  • JesusAlvSoto
    JesusAlvSoto over 7 years
    On ubuntu 14.04.5 and python3 what worked for me was: sudo apt install libpq-dev python3-dev
  • Jorge Vidinha
    Jorge Vidinha over 7 years
    Excelent Thank you so much .
  • kuanb
    kuanb over 7 years
    I did this as well, after exhausting days and all other options that have been described online. I'm still dumbfounded as to what was preventing it from being installed with pip in venv. I was able to install outside of the virtual environment, no problem. I then literally copied that file from my Python package library directory and pasted within the same within the venv. My hunch is that, becuase my version(s) of Python are Anaconda distributions, that there was something amiss with the path that was being assumed/used.
  • Jorge Leitao
    Jorge Leitao about 7 years
    This does not install the postgresql in the virtualenv like the OP asked.
  • nichochar
    nichochar about 7 years
    I don't think OP necessarily wanted postgres in the virtualenv. He said he is using virtualenv. I think he misunderstood that postgresql is a bigger service than simply a python library.
  • Mark Edington
    Mark Edington about 7 years
    For CentOS 7 and Python 3.4 I had to install "python34-devel". My answer to a related question: stackoverflow.com/a/42370489/26219
  • Mark Edington
    Mark Edington about 7 years
    If you are using Python 3 you'll need to install numbered Python devel packages. See: stackoverflow.com/a/42370489/26219
  • ishandutta2007
    ishandutta2007 about 7 years
    On OSX 10.12.3 (Sierra): brew install postgresql PATH=$PATH:/usr/local/Cellar/postgresql/9.6.2/bin/ pip install psycopg2
  • Harry Moreno
    Harry Moreno about 7 years
    I got this working with database listed in psql -l, host localhost, default database port 5432, user listed with \du once you get into psql and guessing the password.
  • Ben Wheeler
    Ben Wheeler over 6 years
    Worked on AWS EC2, with RHEL-based 64bit Amazon Linux/2.5.1
  • sg28
    sg28 about 6 years
    Saved me ,For My Mac 10.13.3 ,python -v :2.7.10 pip -version :10.0.0 .The pip install psycopg2 was not working
  • LoMaPh
    LoMaPh about 5 years
    For Python3 in Fedora use sudo yum install python36-devel (replace 36 with your version of Python 3).
  • Palak Bansal
    Palak Bansal almost 5 years
    Works! Thanks a lot, I spent too much time on this.
  • Axis
    Axis almost 5 years
    Best answer for Mac OS user
  • Bisonbleu
    Bisonbleu almost 5 years
    On OS X 10.14, this tuned out to be very challenging. See detailed solution here: stackoverflow.com/a/57014480/2162844
  • joar
    joar over 4 years
    @Tessaracter It's accepted because it's the correct answer. You can't install libpq-dev and python-dev with pip, you need to install both libpq-dev and python-dev before you run pip install psycopg2.
  • Mustafa
    Mustafa over 4 years
    Best and simple answer!
  • Arbazz Hussain
    Arbazz Hussain over 4 years
    This should be the answer, sudo apt-get install python-x.x-dev fixed the issue.
  • Fawwaz Yusran
    Fawwaz Yusran over 4 years
    This is not working for python 3.8, python 3.7 worked well.
  • joar
    joar about 4 years
    @FawwazYusran You might have to install python3.8-dev or a similarly named package.
  • Sarthak Kumar
    Sarthak Kumar almost 4 years
    Works on ubuntu 20.04. Thank you!
  • Devaski
    Devaski over 2 years
    sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/‌​bin python3 -m pip install psycopg2 this worked
  • Timothyjames67
    Timothyjames67 over 2 years
    So is Option 1 necessary and THEN Debian/Ubuntu step? or is that Debian/Ubuntu step a part of option 2?
  • joar
    joar over 2 years
    @Timothyjames67 Everything below "Option 2" are specifics about "Option 2", as indicated using both h1/h2/h3 and <hr>
  • nurettin
    nurettin over 2 years
    Option #1 works on pypy as well.
  • Shmack
    Shmack over 2 years
    Maybe worth mentioning that this installs the dependencies for psycopg2 and doesn't pip install it, so you actually have to pip install it after you install the dependencies.
  • IllegalSkillsException
    IllegalSkillsException over 2 years
    You sir, saved my life !
  • lolu
    lolu about 2 years
    I wish this had more upvotes! I've missed this like 3 times. worked fine thank you