title: Connect to a PostgreSQL database on a Docker container
body: <p>I want to run an application with a PostgreSQL database and a REST API powered by Django on separate Docker containers. So far the API has been running on Docker connecting to a SQLite database, but I'm having trouble now that I want to connect to a PostgreSQL database instead.</p>
  
  <p>Docker's <code>docker-compose.yml</code> file:</p>
  
  <pre><code>version: '2'
  services:
      postgres:
          image: postgres
      api:
          build: .
          command: bash -c "python manage.py migrate &amp;&amp; python manage.py runserver 0.0.0.0:1337"
          volumes:
              - .:/usr/src/app
          ports:
              - "1337:1337"
          depends_on:
              - postgres
  </code></pre>
  
  <p>Django's <code>settings.py</code> (using the default settings which the base <code>postgres</code> image works with according to the documentation):</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql',
          'NAME': 'wgomanager',
          'USER': 'postgres',
          'PASSWORD': 'mysecretpassword',
          'HOST': 'localhost',
          'PORT': '5432',
      }
  }
  </code></pre>
  
  <p>When I launch the application with <code>docker-compose up</code> eventually this error is thrown:</p>
  
  <pre><code>api_1       |     connection = Database.connect(**conn_params)
  api_1       |   File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
  api_1       |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  api_1       | django.db.utils.OperationalError: could not connect to server: Connection refused
  api_1       |   Is the server running on host "localhost" (::1) and accepting
  api_1       |   TCP/IP connections on port 5432?
  api_1       | could not connect to server: Connection refused
  api_1       |   Is the server running on host "localhost" (127.0.0.1) and accepting
  api_1       |   TCP/IP connections on port 5432?
  api_1       |
  orchestrator_api_1 exited with code 1
  </code></pre>
  
  <p>What am I doing wrong?</p>