title: Could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
body: <p>Docker version : 19.03.12</p>
  <p>Postgres version : latest</p>
  <p>PHP : 7.4</p>
  <p>Project in Symfony</p>
  <p>When I start <code>docker-compose build</code> all is OK</p>
  <p>But when I would like <code>up</code> the project, I have an error :
  <code>SQLSTATE[08006] [7] could not connect to server: Connection refused  Is the server running on host &quot;127.0.0.1&quot; and accepting TCP/IP connections on port 5432?</code></p>
  <p>docker-compose.yml:</p>
  <pre><code>version: '3.4'
  services:
    php:
      build:
        context: ./kapitain
        dockerfile: Dockerfile-php
      volumes:
        - ./kapitain:/var/www/html
      depends_on:
        - db
    db:
      image: postgres:latest
      restart: always
      environment:
        - POSTGRES_DB=kapitain
        - POSTGRES_USER=kapitain_user
        - POSTGRES_PASSWORD=kapitain_user_password
      volumes:
        - ./kapitain/docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
        - db-data:/var/lib/postgresql/data:rw
      ports:
        - &quot;5432:5432&quot;
    api:
      build:
        context: ./kapitain
        dockerfile: Dockerfile-nginx
      ports:
        - &quot;80:8080&quot;
      volumes:
        - ./kapitain/public:/var/www/html/public:ro
      depends_on:
        - php
  volumes:
    db-data: {}
  </code></pre>
  <p>Dockerfile-php :</p>
  <pre><code>ARG PHP_VERSION=7.4
  
  FROM php:${PHP_VERSION}-fpm-alpine
  
  RUN wget https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.5.1.1-1_amd64.apk &amp;&amp; \
      wget https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.5.1.1-1_amd64.apk
  
  # persistent / runtime deps
  RUN apk add --no-cache \
          acl \
          fcgi \
          file \
          gettext \
          git \
          gnupg \
          --allow-untrusted msodbcsql17_17.5.1.1-1_amd64.apk \
          --allow-untrusted mssql-tools_17.5.1.1-1_amd64.apk \
      ;
  
  ARG APCU_VERSION=5.1.18
  RUN set -eux; \
      apk add --no-cache --virtual .build-deps \
          $PHPIZE_DEPS \
          icu-dev \
          libzip-dev \
          postgresql-dev \
          zip \
          zlib-dev \
          unixodbc-dev \
      ; \
      \
      docker-php-ext-configure zip; \
      docker-php-ext-install -j$(nproc) \
          intl \
          pdo_pgsql \
          pdo_mysql \
          zip \
      ; \
      pecl install \
          apcu-${APCU_VERSION} \
          xdebug \
          pdo_sqlsrv \
      ; \
      pecl clear-cache; \
      docker-php-ext-enable \
          apcu \
          opcache \
          xdebug \
          pdo_sqlsrv \
      ; \
      \
      runDeps=&quot;$( \
          scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
              | tr ',' '\n' \
              | sort -u \
              | awk 'system(&quot;[ -e /usr/local/lib/&quot; $1 &quot; ]&quot;) == 0 { next } { print &quot;so:&quot; $1 }' \
      )&quot;; \
      apk add --no-cache --virtual .api-phpexts-rundeps $runDeps; \
      \
      apk del .build-deps
  
  RUN rm msodbcsql17_17.5.1.1-1_amd64.apk &amp;&amp; \
      rm mssql-tools_17.5.1.1-1_amd64.apk
  
  COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
  
  RUN ln -s $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini
  
  # https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
  ENV COMPOSER_ALLOW_SUPERUSER=1
  # install Symfony Flex globally to speed up download of Composer packages (parallelized prefetching)
  RUN set -eux; \
      composer global require &quot;symfony/flex&quot; --prefer-dist --no-progress --no-suggest --classmap-authoritative; \
      composer clear-cache
  ENV PATH=&quot;${PATH}:/root/.composer/vendor/bin&quot;
  
  WORKDIR /var/www/html
  
  # build for production
  ARG APP_ENV=prod
  
  # prevent the reinstallation of vendors at every changes in the source code
  COPY composer.json composer.lock symfony.lock ./
  RUN set -eux; \
      composer install --no-dev --prefer-dist --no-scripts --no-progress --no-suggest; \
      composer clear-cache
  
  # do not use .env files in production
  COPY .env ./
  COPY .env.test ./
  COPY behat.yml.dist ./behat.yml
  RUN composer dump-env ${APP_ENV}; \
      rm .env; \
      rm .env.test
  
  # copy only specifically what we need
  COPY bin bin/
  COPY config config/
  COPY public public/
  COPY src src/
  COPY features features/
  COPY fixtures fixtures/
  COPY templates templates/
  COPY translations translations/
  
  RUN set -eux; \
      mkdir -p var/cache var/log; \
      composer dump-autoload --no-dev --classmap-authoritative; \
      composer run-script --no-dev post-install-cmd; \
      chmod +x bin/console; sync
  
  COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
  RUN chmod +x /usr/local/bin/docker-entrypoint
  
  RUN chown -R www-data:www-data /var/www/html
  USER www-data:www-data
  
  ENTRYPOINT [&quot;docker-entrypoint&quot;]
  CMD [&quot;php-fpm&quot;]
  </code></pre>
  <p>DATABASE_URL in .env :
  <code>DATABASE_URL=pgsql://kapitain_user:kapitain_user_password@127.0.0.1:5432/kapitain</code></p>
  <p>netstat -at :
  <code>TCP    0.0.0.0:5432           PGLW0321:0             LISTENING       InHost</code></p>
  <p>Someone can help me please?</p>
  <p>Best regards</p>