Can’t connect to database via pgcli

Hey guys,

I hope this is the right sub.

I created a db with the following command:

docker run -it \
  -e POSTGRES_USER="root" \
  -e POSTGRES_PASSWORD="root" \
  -e POSTGRES_DB="ny_taxi" \
  -v $(pwd)/ny_taxi_postgres_data:/var/lib/postgresql/data \
  -p 5432:5432 \
  postgres:13

This is working fine. Now I want to access the database via pgcli and I get the following error:

❯ pgcli -h localhost -p 5432 -U root -d ny_taxi
connection failed: :1), port 5432 failed: FATAL:  role "root" does not exist

But when I run

docker exec -it <container_name> /bin/bash 

and then

psql -h localhost -d ny_taxi -U root

I get access:

❯ docker exec -it focused_blackwell /bin/bash
root@14ee944f84dc:/# psql -h localhost -d ny_taxi -U root
psql (13.9 (Debian 13.9-1.pgdg110+1))
Type "help" for help.

ny_taxi=#

Can anybody help me?
Thank you!

1 Like

I am facing the same issue as u

Hello

I think you need to create a network first. This with docker network create xxx

Then, in both docker run instructions, add the --network flag and the name of your network.

Indeed as soon as you want a container being able to connect another container they should be running on the same network.

Read the error, a user does not exist in DB.

Try to use

POSTGRES_USER=root

instead of

POSTGRES_USER="root"

I think the quotes are set wrong.