'postgres in docker container role "postgres" does not exist

  1. Create a postgres docker container with below command - docker run -v /home/ec2-user/Vteck-postgres-data:/var/lib/postgresql/data -d -e POSTGRES_USER=odoo POSTGRES_PASSWORD=odoo -e POSTGRES_DB=postgres --name vteck-db postgres
  2. access to docker container - docker exec -it vteck-db bash
  3. root@f1ba565db798:/# psql -U postgres psql: error: could not connect to server: FATAL: role "postgres" does not exist

but if I create docker container with - docker run --rm -d -e POSTGRES_PASSWORD=root --name postgres postgres, I can successfully access into psql with psql - U postgres.

Any problem on my step 1's command?



Solution 1:[1]

From postgres readme on Docker hub:

POSTGRES_USER

This optional environment variable is used in conjunction with POSTGRES_PASSWORD to set a user and its password. This variable will create the specified user with superuser power and a database with the same name. If it is not specified, then the default user of postgres will be used.

In other words if you specify POSTGRES_USER environment variable, there will be no postgres user. Instead, it will create a user with the name from the environment variable (odoo in your case).

Solution 2:[2]

In your yml file, make a POSTGRES_DB that is NOT 'postgres'

<snip>
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: heavymetaldifficultmoeilijk
      POSTGRES_DB: foodb
<snip>

Connect to your container from the host:

# docker exec -it myproject_pg_container_name /bin/bash

Then, from the container:

root@d14c082e80c8:/# psql -U postgres

Michael

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anemyte
Solution 2 Dharman