Search code examples
postgresqldockersymfony

Cannot connect to postgres dabase running in docker with my credentials


I am following the symfony book on symfony.com to learn symfony. When I create my symfony project I update my docker-compose.yml like this (values for the db name, password and user are just example):

version: '3'

services:
###> doctrine/doctrine-bundle ###
  database:
    image: postgres:${POSTGRES_VERSION:-15}-alpine
    environment:
      POSTGRES_DB: testdb
      # You should definitely change the password in production
      POSTGRES_PASSWORD: 123
      POSTGRES_USER: test
    volumes:
      - database_data:/var/lib/postgresql/data:rw
    ports:
      - "5433:5432"
      # 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:
###> doctrine/doctrine-bundle ###
  database_data:
###< doctrine/doctrine-bundle ###

When running this command in CLI in the project folder:

docker-compose up -d

It will create new docker container running the postgres:postgres docker container

The problem is when I try to connect to the postgres instance running in the container. When I try to connect via PSQL CLI with credentials specified in the docker-compose.yaml I get this error:

psql: error: connection to server at "localhost" (::1), port 57514 failed: FATAL: password authentication failed for user "test"

However I can connect with credentials which were default when I create the symfony project.

Default configuration:

version: '3'

services:
###> doctrine/doctrine-bundle ###
  database:
    image: postgres:${POSTGRES_VERSION:-15}-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-app}
      # You should definitely change the password in production
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!}
      POSTGRES_USER: ${POSTGRES_USER:-app}
    volumes:
      - database_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:
###> doctrine/doctrine-bundle ###
  database_data:
###< doctrine/doctrine-bundle ###

So when I connect with user app and password !ChangeMe! it will connect.

I am on windows 10

I tried to configure the docker-compose.yml but with no succes.


Solution

  • Notice this waning from the postgres image docs:

    the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup

    If you first started this container with some values for POSTGRES_USER, POSTGRES_DB and POSTGRES_PASSWORD, these have been saved into the data directory and will persist even if you start again the container with other values.

    If you want to use another user/password you can

    • Change the user password (docs / example)
    • Add a new user (docs)
    • Remove all containers and linked volumes with the -v flag of docker-compose down (docs, YOU WILL LOSE ALL DATA IN THE VOLUMES!)

    edit: as atish.s pointed out you are also trying to connect to the database on the wrong port (the DB is exposed on 5433, based on the logs you pasted you are trying to connect to 57514)