How to install psycopg2 with "pip" on Python?
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
André
Updated on November 20, 2021Comments
-
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é about 13 yearsThanks for the reply. But I need to install to an virtual environment. Best Regard,
-
Praveen Gollakota about 13 yearsThis trick (courtesy, Ned Batchelder) has helped me in the past to install from binaries to a virtualenv
-
slacy almost 13 yearsIs 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 over 12 years11.04: different errors, the same solution. Worship to dev versions!
-
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 (aspsycopg2
uses thelibpq
andpython
libraries, among others). -
Sam Joseph about 12 yearsI got this error when postgres was not installed on my machine - I installed postgres and the error went away ...
-
xamox about 12 yearsWorks on Ubuntu 11.10 as well. Thanks!
-
caleb almost 12 yearsI'm using Ubuntu 12.04 LTS, and I also had to
sudo apt-get install postgresql-server-dev-all
-
Saher Ahwal almost 12 yearsare you sure that is right? I installed postgres on my windows machine and I still get that. ?
-
Saher Ahwal almost 12 yearscan you elaborate please. Where do you get the psycopg2 at the first place?
-
Saher Ahwal almost 12 yearsWhy don't they have that on their site as pre-req to have postgres?
-
user2471801 almost 12 yearswindows binaries are available at stickpeople.com/projects/python/win-psycopg
-
Pravitha V almost 12 yearshow can i install libpq-dev python-dev packages in opensuse?
-
lajarre over 11 yearsCould someone please explain why not just the directory containing pg_config to the PATH (or use --pg-config)?
-
Seth over 11 yearsWorks for me on Ubuntu 12.10.
-
Duncan Parkes over 11 yearsI found I only needed to install
libpq-dev
, which is fortunate, aspython-dev
wouldn't install. -
jpmc26 almost 11 yearsThis 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 over 10 yearsI'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 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 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 about 10 yearsThis was a lifesaver. Thanks a lot!
-
Ryder Brooks about 10 yearsI 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 about 10 yearssudo PATH=$PATH:/Applications/Postgres93.app/Contents/MacOS/bin pip install psycopg2 [as of April 5, 2014]
-
tbradley22 almost 10 yearsnichochar'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 over 9 yearsConfirmed for Postgres.app 9.3.5 also :)
-
Richard D over 9 yearsWith 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 over 9 yearsif 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 over 9 yearsMy case OS X Ver 10.9.4: sudo PATH=$PATH:/Library/PostgreSQL/9.2/bin pip install psycopg2
-
BrockLee over 9 yearsThank you for this. I was about to go insane before I found this!
-
seanv507 over 9 yearsthe stickpeople link also discusses virtual env and points to a pip install version github.com/nwcell/psycopg2-windows
-
iamfrancisyo over 9 yearsYes, 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 about 9 yearsAnd about Os x? I mean, is based on Unix but I think can be different.
-
Bret about 9 yearsAny idea why this doesn't end up in the default path?
-
Dave about 9 yearsI'm using a windows and inside a virtualenv and the easy_install psycopg2 worked like a charm.
-
Gordon Linoff almost 9 yearsAs a note: this is an excellent example of why post-only answers should not be allowed. The link no longer works.
-
jayeshkv almost 9 yearsi tried this and i get psycopg2-2.6-cp34-none-win_amd64.whl is not a supported wheel on this platform.
-
qwerty almost 9 yearsThanks, 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 almost 9 yearsWhy from source instead of Homebrew?
-
duozmo almost 9 yearsWorked 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 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 almost 9 yearsI 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/setup.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 over 8 yearsIt's not a good practice to mix pip packages and apt-get packages.
-
grim over 8 yearsNote that I didn't need to update the PATH environment variable.
-
radtek over 8 yearsYou shouldn't have to if you reload your terminal session.
-
derrend over 8 yearsIf 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 over 7 yearsUse the latest version: PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin pip install psycopg2
-
zelusp over 7 yearsOnly @seanv507 comment solved my problem, however that linked repo is abandoned. Checkout this link for an updated solution (psycopg2.6.2 + Win64 + env)
-
Storm over 7 yearsI had to do sudo apt-get install libpq-dev python3-dev as im using python3
-
JesusAlvSoto over 7 yearsOn ubuntu 14.04.5 and python3 what worked for me was: sudo apt install libpq-dev python3-dev
-
Jorge Vidinha over 7 yearsExcelent Thank you so much .
-
kuanb over 7 yearsI 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 thevenv
. 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 about 7 yearsThis does not install the postgresql in the
virtualenv
like the OP asked. -
nichochar about 7 yearsI 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 about 7 yearsFor 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 about 7 yearsIf you are using Python 3 you'll need to install numbered Python devel packages. See: stackoverflow.com/a/42370489/26219
-
ishandutta2007 about 7 yearsOn OSX 10.12.3 (Sierra):
brew install postgresql PATH=$PATH:/usr/local/Cellar/postgresql/9.6.2/bin/ pip install psycopg2
-
Harry Moreno about 7 yearsI 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 over 6 yearsWorked on AWS EC2, with RHEL-based 64bit Amazon Linux/2.5.1
-
sg28 about 6 yearsSaved 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 about 5 yearsFor Python3 in Fedora use
sudo yum install python36-devel
(replace 36 with your version of Python 3). -
Palak Bansal almost 5 yearsWorks! Thanks a lot, I spent too much time on this.
-
Axis almost 5 yearsBest answer for Mac OS user
-
Bisonbleu almost 5 yearsOn OS X 10.14, this tuned out to be very challenging. See detailed solution here: stackoverflow.com/a/57014480/2162844
-
joar over 4 years@Tessaracter It's accepted because it's the correct answer. You can't install
libpq-dev
andpython-dev
with pip, you need to install bothlibpq-dev
andpython-dev
before you runpip install psycopg2
. -
Mustafa over 4 yearsBest and simple answer!
-
Arbazz Hussain over 4 yearsThis should be the answer,
sudo apt-get install python-x.x-dev
fixed the issue. -
Fawwaz Yusran over 4 yearsThis is not working for python 3.8, python 3.7 worked well.
-
joar about 4 years@FawwazYusran You might have to install python3.8-dev or a similarly named package.
-
Sarthak Kumar almost 4 yearsWorks on ubuntu 20.04. Thank you!
-
Devaski over 2 yearssudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin python3 -m pip install psycopg2 this worked
-
Timothyjames67 over 2 yearsSo is Option 1 necessary and THEN Debian/Ubuntu step? or is that Debian/Ubuntu step a part of option 2?
-
joar over 2 years@Timothyjames67 Everything below "Option 2" are specifics about "Option 2", as indicated using both h1/h2/h3 and <hr>
-
nurettin over 2 yearsOption #1 works on pypy as well.
-
Shmack over 2 yearsMaybe 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 over 2 yearsYou sir, saved my life !
-
lolu about 2 yearsI wish this had more upvotes! I've missed this like 3 times. worked fine thank you