Docker Community Forums

Share and learn in the Docker community.

Connecting container with PostgreSQL Database on localhost / host machine - macOS Catalina- 10.15

Hello I am new to docker, I have an existing python Django todo app which is connected to localhost PostgreSQL database, Now I want to run Django todo app inside container while connecting with the existing database on localhost.

here are some details:
OS = macOS Catalina
docker = 20+
python = 3.9.2
Django = 3.0
postgres /psql = 13.2
container_name = django
image_name = django-apps

have updated following files
postgresql.conf: updated #listen_addresses = “localhost” to listen_addresses = “*”
pg_hba.conf: added host MyDB all md5

Inside Django project mysite-> have added the database connection details

I am able to make build of the image, but don’t know how to connect with the database, I have tried following commands but aren’t working for me :

docker run -p 8000:80 --add-host=host.docker.internal --name <container name> <image_name>
docker run -p 8000:80 --add-host=host.docker.internal --name django django-apps
It gives me

bad argument


docker run -p 8000:80 --name <container name> <image_name>
then I do get error →

psycopg2.operationError: could not connect to server: Connection refused
Is the server running on the host “” and accepting TCP/IP connections on port 5432?

docker run -p 8000:80 -e "DB_HOST=" -e "DB_NAME=MyDB" -e "DB_USER=djangopostgres"-e "DB_PASS=<password>" -e "DB_PORT=5432" --name <container name> <image_name>

Even above command didn’t helped don’t know where I am missing something.

Please Note: I don’t want to create new container for postgres as of now

please let me know if anyother details are needed

Use as IP address range, not
Don’t use localhost to connect to the PostgreSQL database on your host, but the host’s IP instead. To keep the container portable, start the container with the --add-host=database: flag and use database as hostname for connecting to PostgreSQL.
Make sure PostgreSQL is configured to listen for connections on all IP addresses, not just on localhost. Look for the setting listen_addresses in PostgreSQL’s configuration file, typically found in /etc/postgresql/9.3/main/postgresql.conf (credits to @DazmoNorton).
Long version is not a range of IP addresses, but a single address (namly No Docker container will ever get that address assigned, because it’s the network address of the Docker bridge (docker0) interface.

When Docker starts, it will create a new bridge network interface, that you can easily see when calling ip a:

$ ip a

3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
inet scope global docker0
valid_lft forever preferred_lft forever
As you can see, in my case, the docker0 interface has the IP address with a netmask of /16 (or This means that the network address is

The IP address is randomly assigned, but without any additional configuration, it will always be in the network. For each Docker container, a random address from that range will be assigned.

This means, if you want to grant access from all possible containers to your database, use