postgresqldockerdocker-compose

docker-compose not setting psql user or db


I have a project with only this compose.yml

services:
  postgresql:
    image: postgres:latest
    container_name: postgresql
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: test_db

But when I use table plus or pgadmin4 to view I get an error about the user user. I can connect with pgadmin using postgres as the user though. So it looks like it only creates a postgres user and the default postgres db?

I'm getting this error

connection to server at "127.0.0.1", port 5432 failed: FATAL:  role "user" does not exist

This exact file was creating user and test_db a week ago. So I'm not sure what has changed. It's the only file in the project aside from a nuke shell command

#!/bin/bash

# Stop and remove all containers
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)

# Remove all volumes
docker volume rm $(docker volume ls -q)

# Remove all images
docker rmi $(docker images -aq)

this makes sure there are no volumes, images, or containers

am I missing something? Shouldn't

      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: test_db

create the user and test_db?

========

docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS                     PORTS     NAMES
632d2de6ce44   postgres:latest   "docker-entrypoint.s…"   46 seconds ago   Exited (0) 2 seconds ago             postgresql

Solution

  • As the answer was resolved in the comment section i just want to explain what was the OP's issue:

    default port 5432 for postgres in host machine was occupied by some other service, pobably another running container

    for verification: The diffrent host port was bind done with pg container default port i.e 5433:5432

    services:
      postgresql:
        image: postgres:latest
        container_name: postgresql
        ports:
          - "5433:5432"
        environment:
          POSTGRES_USER: user
          POSTGRES_PASSWORD: password
          POSTGRES_DB: test_db
    

    Therfore service started running as expected, so it was concluded the host port 5432 was occupied.

    what to take from here:

    always try to bind your host port to any other open port in your machine if you default port bind dont work for verification when you face similar issue