0

My attempt to start a PostgreSQL Docker container fails when doing docker-compose. I can connect to it if I do docker run instead. I don't know what the difference is. My intent with Compose is to have it create a database usable for development and that I can connect to with user postgres.

docker-compose.yml:

version: '3.7'
services:
  my_app:
    build:
      dockerfile: ./dockerfile-my-app
      context: .
    command:  tail -f /dev/null
    depends_on:
      - db
    env_file: ./.env.development
    ports:
      - "8080:8080"
    volumes:
      - /c/Users/woodsman/linux-mint-woodsman/home/dev:/home/dev

  db:
    image: postgres:latest
    env_file: ./.env.development
    environment:
      - POSTGRES_USER= postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=helloworld
      - PGDATA=/var/lib/postgresql/data/helloworld2/


    ports:
      - "5432:5432"
    restart: "no"
    volumes:
      - /c/Users/woodsman/linux-mint-woodsman/dbdata:/var/lib/postgresql/data/pgdata


volumes:
  dbdata:

Attempting to connect to jdbc:postgresql://localhost:5432/helloworld using user postgres and password postgres. Please pardon any security concerns you may perceive. This will be tightened for development, and done much more securely when deployed to production.

The reported error as reported by SquirrelSQL is:

hello-world: FATAL: password authentication failed for user "postgres"
class org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"

The image hashcode is: 9dbc24674f25eb449df11179ed3717c47348fb3aa985ae14b3936d54c2c09dde

>docker logs c4c2
2022-05-16 05:28:50.560 UTC [1] LOG:  starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-05-16 05:28:50.560 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2022-05-16 05:28:50.560 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2022-05-16 05:28:50.564 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-05-16 05:28:50.571 UTC [63] LOG:  database system was shut down at 2022-05-16 05:28:50 UTC
2022-05-16 05:28:50.579 UTC [1] LOG:  database system is ready to accept connections
2022-05-16 05:29:17.184 UTC [70] FATAL:  password authentication failed for user "postgres"
2022-05-16 05:29:17.184 UTC [70] DETAIL:  Role "postgres" does not exist.
        Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
2022-05-16 05:36:41.983 UTC [78] FATAL:  password authentication failed for user "postgres"
2022-05-16 05:36:41.983 UTC [78] DETAIL:  Role "postgres" does not exist.
        Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"

I am seeking a Docker based answer. If there's some PostgreSQL command I need to do (or not do), please tell me how I can pass that through Docker.

1
  • 1
    One thing I noticed is that you have PGDATA=/var/lib/postgresql/data/helloworld2/ but you map your volume to /var/lib/postgresql/data/pgdata. I would expect those two paths to be the same. Commented May 16, 2022 at 7:07

2 Answers 2

1

Your main error is:

Role "postgres" does not exist.

It seems because whitespace here in docker-compose.yml:

  • POSTGRES_USER= postgres

In first start postgres was initialized, so when You change "POSTGRES_USER" in variable, role will not create. You can delete you volume and start docker-compose again.

Sign up to request clarification or add additional context in comments.

4 Comments

Let me see if I misunderstood.. I created a username with a space in it?
Yes, i think so. You can try to login as " postgres". You can run psql in your container, like psql -h localhost -p 5432 -U " postgres" -W "postgres" You can see variables in your container, also. "echo $POSTGRES_USER"
Thanks you @P-S. That was not obvious to me. I wish I could pay people like you.
@Woodsman , community stackoverflow helps me a lot in my job, so I try to be helpfull too. After your react to my answer, I can react to anothers answers, it's more then enought for me)
0

For user, who did not misspell their POSTGRES_USER (e.g., myself). This question provides same insightful answers: Could not translate host name "db" to address using Postgres, Docker Compose and Psycopg2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.