Skip to content

Connecting From Django To Postgres When They Are In Different Networks

I have 2 different projects, running them using docker compose. I needed to connect from project1.django to project2.postgres. Here is how I did it...

1. Check the running docker containers

$ docker ps --format "{{.Names}}"     
project1_local_django
project2_local_postgres

2. Check the networks

$ docker network ls                                           
NETWORK ID     NAME                   DRIVER    SCOPE
9eb5f04cf191   project2_default       bridge    local
92f90ca252de   project1_default       bridge    local

3. Connect project1 Containers to project2_default: First, ensure project2 is up and running so that project2_default network is available.

Then, manually connect project1's django container to project2_default network:

$ docker network connect project2_default project1_django_container

4. Ensure that you change the environment variable

POSTGRES_HOST=project2_postgres_container
  • Now we are able to connect from project1.django to the project2.postgres