3

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.

David Kilroy
  • 41
  • 1
  • 1
  • 3
  • Possible duplicate of [Django connection to postgres by docker-compose](https://stackoverflow.com/questions/42811727/django-connection-to-postgres-by-docker-compose) – David Maze Dec 10 '18 at 20:13

2 Answers2

4

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.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks for the response. I actually dont have a postgres container, apologies for not specifying this in the question. This postgres database is on my local computer. Is there any way I can connect to my local Postgres database or do I have to make a container for it. Thanks – David Kilroy Dec 10 '18 at 20:32
  • As @larsks suggested, look at docker networking documentation to understand how container networking works. You don't need to make a postgresql container, but 'localhost' in django container is the container itself. Try connecting to your pc's real ip instead. – gile Dec 10 '18 at 21:13
  • In my case, api is running in docker container while postgres is running on localhost. The error was "cannot assign requested address 5432". The problem was that connections string points to localhost by default. I've changed it to "host.docker.internal" - "IdentityServerDb": "Server=host.docker.internal;Port=5432;Database=postgres;UserId=postgres;Password=xx;" and it worked. – Baglay Vyacheslav Jun 07 '19 at 08:35
0
  1. Make sure this is done

    postgresql.conf- > listen_addresses = '*'

    pg_hba.conf - > host all all 0.0.0.0/0 md5

  2. brew services start postgresql (In MAC, reload postgres Service ).

  3. Get your system Ip.
  4. Use your system ip address instead of localhost in your DB connection.
Ajay Rawat
  • 161
  • 2
  • 12