简体   繁体   中英

Django can't connet to PostgreSQL Docker container via psycopg2

I am trying to migrate a django project on macOS Monterey 12.3, and I am having some troubles.

It seems like psycopg2 doesn't want to connect to my docker container at all. Everytime it prints this error:

django.db.utils.OperationalError: connection to server at "localhost" (127.0.0.1), port 5433 failed: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.

For my docker process, I create it by running

docker run --name local-postgres -p 5433:5433 -e POSTGRES_PASSWORD=test123 -d postgres

I am running python 3.9.12 in a virtual environment using pipenv, and my arch is arm64, for anyone wondering.

I've tried changing the ports, I've tried resetting, completely removing docker and downloading it again, reinstalling django and reinstalling the venv again and so far nothing has worked. I've also tried setting the CONN_MAX_AGE=0 in settings, which has not worked.

Please help

Postgres listens on port 5432, so you need to map that to the port you want to connect to on the host. It looks like you want to use port 5433, so you'd do

docker run --name local-postgres -p 5433:5432 -e POSTGRES_PASSWORD=test123 -d postgres

Then you can connect on the host using localhost port 5433.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM