8
  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 Docker container: docker exec -it vteck-db bash

  3. Connect with the client: 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 connect with psql - U postgres.

Any problem with my first step's command?

2 Answers 2

16

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 put something into the POSTGRES_USER environment variable, the startup script will honor that and create that user instead of the default one (postgres, that is).

Also, this and other POSTGRES_* variables are only used during the initial startup, when the database directory is empty. So if you restart the container with some other value, it won't make any difference (you'll see in logs something about "database seems ok, skipping initialization") unless you manually deleted the database.

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

3 Comments

I'm having the same issue, this helped me understand the issue, thank you, now I'm looking for the command to login to PostgreSQL with odoo, I want to connect to the DB to build a REST API
@NimrSawafta try this docker exec -e PGPASSWORD=odoo -it POSTGRES_CONTAINER_NAME psql -U odoo
Thank you so much @anemyte for your additional comment about intial startup and database directory being empty. This was my issue, I was making changes to fix logging in to the database, but the changes weren't taking affect because it was skipping those initialization steps.
0

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

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.