1

Been trying to implement a django app with postgresql database on heroku, with psycopg2 as the backend.
(Based on the research I've done, psycopg3 looks like it's not really built for that purpose)
My app works, without issue running inside heroku/heroku console, and connects to db fine.

But running locally (python manage.py runserver), it just can't detect the psycopg2 module. I've reinstalled the module in and out of a venv. I've tried the binary install, used -pep517 method as well.
I saw a thread here indicating newer python may not be compatible, but it didn't really provide any concrete proof as far as I could tell.
Any advise in this regard, or perhaps a better backend adapter suggestion for Django + postgres? below are the relevant portions of settings.py in the django project directory:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': '*db info here'
    ...more db creds
}
}

and the error:

File "C:\*userdirectory*\djangenv\Lib\site-packages\django\db\backends\postgresql\base.py", line 24, in <module>
import psycopg2 as Database
File "C:\*userdirectory*\djangenv\Lib\site-packages\psycopg2\__init__.py", line 51, in 
<module> from psycopg2._psycopg import ( # noqa 
ImportError: DLL load failed while importing _psycopg: The specified module could not be 
 found.
4
  • I just use the default sqlite3 db for my local dev work and then use postgres for my prod by doing a sym link with different settings, So I'd recommend just doing that. But if you google ImportError: DLL load failed while importing _psycopg, you'll find others that ran into this issue, specifically on windows, maybe there's a fix that already exists in those questions
    – Nealium
    Oct 28, 2022 at 17:46
  • Try using django.db.backends.postgresql_psycopg2
    – ilyasbbu
    Oct 28, 2022 at 18:19
  • 1
    Also pip install psycopg2-binary
    – ilyasbbu
    Oct 28, 2022 at 18:21
  • I'll definitely look creating a sim link for sqlite3, thanks for the tip! for the other two comments, I have already tested the binary module, inside a venv and outside. I have also tried that change to the 'engine' line. neither work, and the research I've seen elsewhere says that writing out the extra '_psycopg2' line is not needed and has been depreciated Oct 28, 2022 at 18:55

1 Answer 1

2

So... It worked upon downgrading back to Python 3.10. Shoulda tried that earlier lol

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.