I am receiving the following error when connecting my Django docker container to my Postgres database.
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line
130, in connect conn = _connect(dsn,
connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server:
Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
Below is my Dockerfile which run the container
FROM python:3.6
MAINTAINER c15523957
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get -y install libgdal-dev
RUN mkdir -p /usr/src/app
COPY requirements.txt /usr/src/app/
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Here is the code in my settings.py file of my Django code
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'place_loc_db',
'USER': 'place_loc_user',
'PASSWORD': 'abc123',
'HOST': 'localhost',
'PORT': 5432,
}
}
Note: I do not have the postgres database running as a container but on my local computer
Note: The following details are contained in my pg_hba.conf and postgresql.conf files
postgresql.conf- > listen_addresses = '*'
pg_hba.conf - > host all all 0.0.0.0/0 md5
I've read that the following details above open connections on the database.