Reputation: 41
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.
Upvotes: 3
Views: 12429
Reputation: 172
Make sure this is done
postgresql.conf- > listen_addresses = '*'
pg_hba.conf - > host all all 0.0.0.0/0 md5
brew services start postgresql (In MAC, reload postgres Service ).
Upvotes: 0
Reputation: 311238
It looks like you're trying to connect to localhost
. Your Postgres database is notrunning inside the same container as your django app, so you're not going to be able to find it at localhost
. You need to point your app at the address of the Postgres container.
If you run your containers in a user defined network (using docker-compose will do this for you automatically), then you can use container name as hostnames.
The documentation on container networking is a good place to start.
Update
The fact that you're running Postgres on your host doesn't substantially change the answer: you still need to point your webapp at the address of the Postgres server, rather than localhost
.
The easiest way of doing that depends on whether or not you're running Docker natively on Linux, or you're running Docker-For-X, where X is MacOS or Windows.
On Linux, just point your webapp at the ip address of the docker0
interface. This is an address of your host, and since you have Postgres configured to listen on all interfaces, this should work out just fine.
If you're on Mac or Windows, there is a special "magic hostname" that refers to services on your host. E.g., read this for details under MacOS. In both cases, you can point your webapp at host.docker.internal
.
Upvotes: 4