How to solve “root” does not exist in Postgres docker

Problem

By default when we create docker in Postgres, we usually use this command to create a container

$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

after that, we execute to Postgres container

for my case, I run created command

$ created hellodb

And we got an error

createdb: error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: FATAL:  role “root” does not exist

Solution

By default shell use root to execute the command but when we create a Postgres container we assign a user named “postgres”

By this command

$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

So we have to change the user in the shell to user postgres user by this command

$ su - posgres

You will see a user of the shell has been changed. Then you can use createdb command and enter to hellodb normally

Thank for reading & enjoy coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s