Home > database >  Can't connect to Docker Hosted Postgres
Can't connect to Docker Hosted Postgres

Time:05-17

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 the 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 hope for 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.

Thanks, Woodsman

CodePudding user response:

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.

  • Related