title: django.db.utils.OperationalError: could not connect to server: Connection refused
body: <p>I have 3 docker containers web(django) , nginx, db(postgresql)</p>
  <p>When I run the following command</p>
  <pre><code>docker-compose -f docker-compose.prod.yml exec web python manage.py migrate --noinput
  </code></pre>
  <p>The exact error is:</p>
  <pre><code>  django.db.utils.OperationalError: could not connect to server: Connection refused
          Is the server running on host &quot;localhost&quot; (127.0.0.1) and accepting
          TCP/IP connections on port 5432?
      could not connect to server: Address not available
          Is the server running on host &quot;localhost&quot; (::1) and accepting
          TCP/IP connections on port 5432?
  </code></pre>
  <p><strong>docker-compose.prod.yml</strong></p>
  <pre><code>version: '3.7'
  
  services:
    db:
      image: postgres:12.0-alpine
      volumes:
        - postgres_data:/var/lib/postgresql/data/
      env_file:
        - ./.envs/.db
    web:
      build:
        context: ./tubscout
        dockerfile: Dockerfile.prod
      command: gunicorn hello_django.wsgi:application --bind 0.0.0.0:8000
      volumes:
        - .static_volume:/home/app/web/staticfiles
      expose:
        - 8000
      env_file:
        - ./.envs/.prod
      depends_on:
        - db
    nginx:
      build: ./nginx
      volumes:
        - .static_volume:/home/app/web/staticfiles
      ports:
        - 1337:80
      depends_on:
        - web
  
  volumes:
    postgres_data:
    static_volume:
  </code></pre>
  <p><strong>Dockerfile.prod</strong></p>
  <pre><code>###########
  # BUILDER #
  ###########
  
  # pull official base image
  FROM python:3.8.3-alpine as builder
  
  # set work directory
  WORKDIR /usr/src/app
  
  # set environment variables
  ENV PYTHONDONTWRITEBYTECODE 1
  ENV PYTHONUNBUFFERED 1
  
  # install psycopg2 dependencies
  RUN apk update \
      &amp;&amp; apk add postgresql-dev gcc python3-dev musl-dev 
  
  # install dependencies
  RUN pip install --upgrade pip
  COPY ./requirements.txt .
  RUN pip wheel --no-cache-dir --no-deps -w /usr/src/app/wheels -r requirements.txt
  
  #########
  # FINAL #
  #########
  
  # pull official base image
  FROM python:3.8.3-alpine
  
  # create directory for the app user
  RUN mkdir -p /home/app
  
  # create the app user
  RUN addgroup -S app &amp;&amp; adduser -S app -G app
  
  # create the appropriate directories
  ENV HOME=/home/app
  ENV APP_HOME=/home/app/web
  RUN mkdir $APP_HOME
  RUN mkdir $APP_HOME/staticfiles
  WORKDIR $APP_HOME
  
  # install dependencies
  RUN apk update &amp;&amp; apk add libpq
  COPY --from=builder /usr/src/app /wheels
  COPY --from=builder /usr/src/app/requirements.txt .
  RUN pip install --no-cache /wheels/wheels/*
  
  # copy entrypoint.sh
  COPY ./entrypoint.sh $APP_HOME
  
  # copy project
  COPY . $APP_HOME
  
  # chown all the files to the app user
  RUN chown -R app:app $APP_HOME
  
  # change to the app user
  USER app
  
  # run entrypoint.prod.sh
  ENTRYPOINT [&quot;/home/app/web/entrypoint.sh&quot;]
  </code></pre>
  <p><strong>settings.py</strong></p>
  <pre><code>DATABASES = {
      'default': {
          &quot;ENGINE&quot;: os.environ.get(&quot;SQL_ENGINE&quot;, &quot;django.db.backends.sqlite3&quot;),
          &quot;NAME&quot;: os.environ.get(&quot;SQL_DATABASE&quot;, os.path.join(BASE_DIR, &quot;db.sqlite3&quot;)),
          &quot;USER&quot;: os.environ.get(&quot;SQL_USER&quot;, &quot;user&quot;),
          &quot;PASSWORD&quot;: os.environ.get(&quot;SQL_PASSWORD&quot;, &quot;password&quot;),
          &quot;HOST&quot;: os.environ.get(&quot;SQL_HOST&quot;, &quot;localhost&quot;),
          &quot;PORT&quot;: os.environ.get(&quot;SQL_PORT&quot;, &quot;5432&quot;),
      }
  }
  </code></pre>
  <p><strong>./.envs/.db</strong></p>
  <pre><code>POSTGRES_USER=postgres
  POSTGRES_PASSWORD=123456789
  POSTGRES_DB=tubscoutdb_prod
  </code></pre>
  <p><strong>./.envs/.prod</strong></p>
  <pre><code>DEBUG=0
  SECRET_KEY='#yinppohul88coi7*f+1^_*7@o9u@kf-sr*%v(bb7^k5)n_=-h'
  DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
  SQL_ENGINE=django.db.backends.postgresql
  SQL_DATABASE=tubscoutdb_prod
  SQL_USER=postgres
  SQL_PASSWORD=123456789
  SQL_HOST=localhost
  SQL_PORT=5432
  DATABASE=postgres
  </code></pre>