Postgres container not starting for docker desktop windows

Hi,

My name is Pristal Jane. I have just started using Docker.

I was setting up postgres using docker compose.
Here is the file.

version: "3.8"
services: 
  postgres: 
    image: "postgres:14.1-alpine"
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: "abc"
      POSTGRES_PASSWORD: "abc"
      POSTGRES_DB: "default"
    volumes:
      - d:/postgressql/data:/var/lib/postgresql/data

When I run this using command: docker-compose up -d
container is up and running. Even I am able to access it from pgAdmin.

But when I am entering into the container, I am not able to.

I am getting this error.

D:\kube\postgres>docker exec -it c5c15f5feb30 bash
bash-5.1# psql -h localhost
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  role "root" does not exist
bash-5.1# psql -U abc
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  user "abc" does not exist

Can someone please help? I really need this.

You forgot to set the dbname for the CLI

psql -U abc -d default

I guess you configured it when you connected to postgres from pgAdmin.

Thank you for the reply @rimelek
But I have already tried that & its not working.

CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                    NAMES
a98cbee266a5   postgres:14.1-alpine   "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   0.0.0.0:5432->5432/tcp   postgres-postgres-1

D:\kube\postgres>docker exec -it a98cbee266a5 bash
bash-5.1# psql -U abc -d default
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "abc" does not exist
bash-5.1#

Am I forgetting to open some port or something or some privilege issue?

I tried your compose file and got the same error message as you. Then I fixed that with the command I quoted. Recreating the whole project with docker compose down and removing the data in the host folder could solve some problem caused an already initialized postgres, because the variables would not change anything after that, but you wrote you could access the database from pgAdmin.

This error message is strange, since you set the user to be “abc” and the database as “default”, yet psql says “abc” does not exist as a database. In your first post you mentioned you got this

which is exactly what I got without passing the name of the database. psql should not complain about a missing database with a name that you did not even referred to.

Let’s say it is a strange error which is caused by the fact that you mounted a folder from the Windows host. I can’t imagine that since you could not connect to the dataase from anywhere, but you can test it by removing that mounting and using a named volume which will be saved in the VM.

version: "3.8"

volumes:
  data:

services: 
  postgres: 
    image: "postgres:14.1-alpine"
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: "abc"
      POSTGRES_PASSWORD: "abc"
      POSTGRES_DB: "default"
    volumes:
      - data:/var/lib/postgresql/data

Don’t forget to run docker compose down before docker compose up -d to make sure the new volume will be properly mounted.

Thank you so much @rimelek . It worked.
But there are some strange issues.

version: "3.8"
volumes:
  data:
services: 
  postgres: 
    image: "postgres:14.1-alpine"
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: "pristaljane"
      POSTGRES_PASSWORD: "pristaljane"
      POSTGRES_DB: "watermelon"
    volumes:
      - data:/var/lib/postgresql/data

This is my dockerfile. Also please tell me where can find data volume in my windows machine. This is permanent, right? If I shut the docker down or kill the container, it will stay?

D:\kube\postgres>docker exec -it 1be8011554c2 bash
bash-5.1# psql -U pristaljane
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  database "pristaljane" does not exist
bash-5.1# psql -U pristaljane -d watermelon
psql (14.1)
Type "help" for help.

watermelon=# \du
                                    List of roles
  Role name  |                         Attributes                         | Member of
-------------+------------------------------------------------------------+-----------
 pristaljane | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

watermelon=# \du+
                                           List of roles
  Role name  |                         Attributes                         | Member of | Description
-------------+------------------------------------------------------------+-----------+-------------
 pristaljane | Superuser, Create role, Create DB, Replication, Bypass RLS | {}        |

watermelon=# \l
                                      List of databases
    Name    |    Owner    | Encoding |  Collate   |   Ctype    |      Access privileges
------------+-------------+----------+------------+------------+-----------------------------
 postgres   | pristaljane | UTF8     | en_US.utf8 | en_US.utf8 |
 template0  | pristaljane | UTF8     | en_US.utf8 | en_US.utf8 | =c/pristaljane             +
            |             |          |            |            | pristaljane=CTc/pristaljane
 template1  | pristaljane | UTF8     | en_US.utf8 | en_US.utf8 | =c/pristaljane             +
            |             |          |            |            | pristaljane=CTc/pristaljane
 watermelon | pristaljane | UTF8     | en_US.utf8 | en_US.utf8 |
(4 rows)

watermelon=#

When I entered with first command, it says database pristaljane do not exist, although database name in docker-compose file was waermelon.
This is strange. I do not know why it’s coming.

Is this normal or some thing we need to erase?

Sorry I could not get back sooner.

You mean the compose file.

In the docker-desktop-data WSL2 distribution. Sorry, I try to write as quick as I can, so I can’t search for it now, but we talked about it on the forum multiple times, so I think you could find it.

Yes, but I like to use bind mounts as well, since it is much easier to remove a named volume acidentally than a folder.

It doesn’t matter for the client what you defined in the compose file. Those variables are for the initial setup of the Postgres server. I am not a postgres expert, but I guess you need to define the name of the database if it is not the same as the username.