[Answered ]-How to connect locally hosted Django app to Dockerized Postgres database

1👍

You can simplify the mapping port on docker-compose.yml, remapping the 5432 to 5433 to host.

With this configuration I’m able to connect to pfcd database with dbeaver

version: "3.7"
   
services:
  db:
    container_name: customer_platform_database_local
    image: postgres
 volumes:
      - postgresPFC:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=pfcdb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    hostname: db
    ports:
      - 5433:5432
    restart: always
volumes:
  postgresPFC:

For the connection error I suspect the error is on the driver name
not django.db.backends.postgresql but django.db.backends.postgresql_psycopg2 notice the final psycopg2

See this article and the official django host docs

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'pfcdb',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
        'PORT': 5433,
    }
}

You must also check if you have another service or Postgres instance running on your host on the same port 5433.

👤Max

Leave a comment