You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于dunglas/symfony-docker搭建项目PHP无法连通PostgreSQL问题

PostgreSQL身份验证失败问题排查(symfony-docker模板)

基于GitHub模板dunglas/symfony-docker开发项目,接入postgres数据库时出现PHP与PostgreSQL通信异常问题。

报错信息

执行Symfony迁移操作时Doctrine返回错误:

password authentication failed for user "postgres"

PHP服务日志持续输出等待数据库就绪的提示:

php_1 | Still waiting for db to be ready... Or maybe the db is not reachable.

相关配置

docker-compose.yml

version: "3.4"

services:
  php:
    links:
      - database
    build:
      context: .
      target: symfony_php
      args:
        SYMFONY_VERSION: ${SYMFONY_VERSION:-}
        SKELETON: ${SKELETON:-symfony/skeleton}
        STABILITY: ${STABILITY:-stable}
    restart: unless-stopped
    volumes:
      - php_socket:/var/run/php
    healthcheck:
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 30s
    environment:
      # Run "composer require symfony/orm-pack" to install and configure Doctrine ORM
      DATABASE_URL: ${DATABASE_URL}
      # Run "composer require symfony/mercure-bundle" to install and configure the Mercure integration
      MERCURE_URL: ${CADDY_MERCURE_URL:-http://caddy/.well-known/mercure}
      MERCURE_PUBLIC_URL: https://${SERVER_NAME:-localhost}/.well-known/mercure
      MERCURE_JWT_SECRET: ${CADDY_MERCURE_JWT_SECRET:-!ChangeMe!}

  caddy:
    build:
      context: .
      target: symfony_caddy
    depends_on:
      - php
    environment:
      SERVER_NAME: ${SERVER_NAME:-localhost, caddy:80}
      MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeMe!}
      MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeMe!}
    restart: unless-stopped
    volumes:
      - php_socket:/var/run/php
      - caddy_data:/data
      - caddy_config:/config
    ports:
      # HTTP
      - target: 80
        published: 80
        protocol: tcp
      # HTTPS
      - target: 443
        published: 443
        protocol: tcp
      # HTTP/3
      - target: 443
        published: 443
        protocol: udp

###> doctrine/doctrine-bundle ###
  database:
    image: postgres:${POSTGRES_VERSION:-13}-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-app}
      # You should definitely change the password in production
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-ChangeMe}
      POSTGRES_USER: ${POSTGRES_USER:-symfony}
    volumes:
      - db-data:/var/lib/postgresql/data:rw
      # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
      # - ./docker/db/data:/var/lib/postgresql/data:rw
###< doctrine/doctrine-bundle ###

volumes:
  php_socket:
  caddy_data:
  caddy_config:

###> doctrine/doctrine-bundle ###
  db-data:
###< doctrine/doctrine-bundle ###

.env 相关配置项

POSTGRES_DB=proximityNL
POSTGRES_PASSWORD=postgres
POSTGRES_USER=postgres
DATABASE_URL="postgresql://postgres:postgres@database:5432/proximityNL?serverVersion=13&charset=utf8"

问题原因

首次启动PostgreSQL容器时,会使用当时的环境变量初始化数据库用户、密码、库名,相关信息会持久化在挂载的db-data卷中。后续修改.env的数据库配置后,旧的卷没有被清理,Postgres不会重新执行初始化逻辑,导致新配置的账号密码和数据库中实际存储的不一致,出现身份验证失败。

解决方案

执行以下命令清理旧的容器、孤立资源和关联存储卷:

docker-compose down --remove-orphans --volumes

执行完成后重新启动服务即可正常连接数据库。


内容的提问来源于stack exchange,提问作者Jonathan Morel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 15:06:03