108π
For MacOS users
After trying all the above methods (which did not work for me on MacOS 10.14), that one worked :
- Install openssl with
brew install openssl
if you donβt have it already. - add openssl path to
LIBRARY_PATH
:
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
- install psycopg2 with pip
pip3 install psycopg2
91π
I had the same problem on Arch linux. I think that itβs not an OS dependant problem. Anyway, I fixed this by finding the outdated packages and updating then.
pip uninstall psycopg2
pip list --outdated
pip install --upgrade wheel
pip install --upgrade setuptools
pip install psycopg2
- [Django]-Django β filtering on foreign key properties
- [Django]-Do I need Nginx with Gunicorn if I am not serving any static content?
- [Django]-CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
61π
I was also getting same error.
Using Python 3.7.3 and pip 19.1.1.
I used following command.
pip install psycopg2-binary==2.8.3
- [Django]-Where does pip install its packages?
- [Django]-Handle `post_save` signal in celery
- [Django]-Default filter in Django admin
21π
TDLR
If you arenβt used to installing Python C-extensions, and psycopg2
isnβt a core part of your work, try
pip install psycopg2-binary
Building Locally
psycopg2 is a C-extension, so it requires compilation when being installed by pip. The Build Prerequisites section of the docs explain what must be done to make installation via pip possible. In summary (for psycopg 2.8.5):
- a C compiler must be installed on the machine
- the Python header files must be installed
- the
libpq
header files must be installed - the
pg_config
program must be installed (it usually comes with thelibpq
headers) and on$PATH
.
With these prerequisites satisfied, pip install psycopg2
ought to succeed.
Installing pre-compiled wheels
Alternatively, pip
can install pre-compiled binaries so that compilation (and the associated setup) is not required. They can be installed like this:
pip install psycopg2-binary
The docs note that
The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements.
but I would suggest that psycopg2-binary
is often good enough for local development work if you are not using psycopg2 directly, but just as a dependency.
Concluding advice
Read the informative installation documentation, not only to overcome installation issues but also to understand the impact of using the pre-compiled binaries in some scenarios.
- [Django]-How to get a favicon to show up in my django app?
- [Django]-Django testing: Test the initial value of a form field
- [Django]-Tailwindcss: fixed/sticky footer on the bottom
18π
I had same problem and this appears to be a Mojave Issue, I was able to resolve with:
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
- [Django]-Django REST Framework: how to substitute null with empty string?
- [Django]-How to combine multiple QuerySets in Django?
- [Django]-How can I chain Django's "in" and "iexact" queryset field lookups?
17π
For MacOS users, this question has the correct solution:
install command line tools if necessary:
xcode-select --install
then
env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
- [Django]-Why doesn't django's model.save() call full_clean()?
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
15π
For Mac OS X users:
1. First check your postgresql path by running this command in terminal:
pg_config
If this fails lookup how to add pg_config to your path.
2. Next install Xcode Tools by running this command in terminal:
xcode-select --install
If you have both those sorted out now try to install psycopg2 again
- [Django]-Specifying limit and offset in Django QuerySet wont work
- [Django]-On Heroku, is there danger in a Django syncdb / South migrate after the instance has already restarted with changed model code?
- [Django]-What does error mean? : "Forbidden (Referer checking failed β no Referer.):"
10π
I was also facing the same after running all the above commands, but the following two commands worked for me:
- Instead of pip, use this:
sudo apt-get install libpq-dev
- then run this command:
pip install psycopg2
- [Django]-How to import csv data into django models
- [Django]-Django TextField and CharField is stripping spaces and blank lines
- [Django]-How do I integrate Ajax with Django applications?
6π
On OS X, I was able to solve this by simply upgrading wheel before installing psycopg2:
pip install --upgrade wheel
- [Django]-Django-reversion and related model
- [Django]-Cancel an already executing task with Celery?
- [Django]-Add custom form fields that are not part of the model (Django)
5π
For OSX Sierra users, it seems that an xcode update is the solution: Can't install psycopg2 package through pip install⦠Is this because of Sierra?
- [Django]-Celery : Execute task after a specific time gap
- [Django]-Django edit user profile
- [Django]-Django β SQL bulk get_or_create possible?
4π
I tried all the above solutions but they did not work for me. What I did was change the psycopg2 version in my requirements.txt file from psycopg2==2.7.4
to psycopg2==2.7.6
- [Django]-Django count RawQuerySet
- [Django]-Django Rest Framework: Dynamically return subset of fields
- [Django]-Check if celery beat is up and running
1π
Is your error message complete? the most encountered reason for failing to install psycopg2 on mac from pip is pg_config is not in path.
by the way, using macports or fink to install psycopg2 is more recommended way, so you donβt have to worry about pg_config, libpq-dev and python-dev.
plus, are using Python 3.5? then upgrage your wheel to > 0.25.0 using pip.
- [Django]-How does Django's nested Meta class work?
- [Django]-H14 error in heroku β "no web processes running"
- [Django]-Django error: got multiple values for keyword argument
1π
I faced the same issue, but the answers above didnβt work for me.
So this is what I did in my requirements.txt
psycopg2-binary==2.7.6.1 and it worked fine
- [Django]-Django REST Framework β 405 METHOD NOT ALLOWED using SimpleRouter
- [Django]-SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend
- [Django]-Django β No such table: main.auth_user__old
1π
I had this issue on several packages, including psycopg2, numpy, and pandas. I simply removed the version from the requirements.txt file, and it worked.
So instead of psycopg2-binary==2.7.6.1 I just had psycopg2-binary.
- [Django]-What is related_name used for?
- [Django]-Rendering a template variable as HTML
- [Django]-How does django handle multiple memcached servers?
1π
sudo apt install libpq-dev python3.X-dev
where X is the sub version,
these should be followed by :
pip install --upgrade wheel
pip install --upgrade setuptools
pip install psycopg2
Enjoy !!!
- [Django]-Django Templating: how to access properties of the first item in a list
- [Django]-Multiple Database Config in Django 1.2
- [Django]-How do I reuse HTML snippets in a django view
1π
Fixed by installing python3.7-dev
: sudo apt install python3.7-dev
, based on the link.
- Python: 3.7
- Ubuntu: 20.04.3 LTS
- [Django]-ImportError: cannot import name 'β¦' from partially initialized module 'β¦' (most likely due to a circular import)
- [Django]-Django Passing Custom Form Parameters to Formset
- [Django]-How to allow users to change their own passwords in Django?
0π
I know you are asking for development environment but if you are deploying on server say, Heroku. Just add below line in the requirements.txt of your project.
django-heroku==0.3.1
As this package itself will install the required packages like psycopg2 on server deployment.So let the server(heroku) should take care of it.
- [Django]-How to automate createsuperuser on django?
- [Django]-Django β how to create a file and save it to a model's FileField?
- [Django]-Django TextField and CharField is stripping spaces and blank lines
0π
I solved my problem by updating/installing vs_BuildTools. The link to the software was given in the error itself.
Error Image
- [Django]-Using a UUID as a primary key in Django models (generic relations impact)
- [Django]-Django: reverse accessors for foreign keys clashing
- [Django]-How to override css in Django Admin?
0π
I though the LIBRARY_PATH would work but unfortunately it didnβt.
Using Homebrew on MacOS Silicon, the following workaround did the trick for me:
LDFLAGS=-L/opt/homebrew/opt/openssl/lib CPPFLAGS=-I/opt/homebrew/opt/openssl/include pip install -r requirements.txt # or pip install psycopg2-binary
- [Django]-What's the best Django search app?
- [Django]-Add additional options to Django form select widget
- [Django]-Where are the Assertion Methods list from Django TestCase?
0π
On windows Iβve managed to solve this by adding C:\Program Files\PostgreSQL\15\lib to LIB env variable
- [Django]-Passing variable urlname to url tag in django template
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
- [Django]-Django import error β No module named core.management