Can't connect to Postgres through Docker

I have created a container with the following Postgres db but when I try to connect to it using pgcli I get this error. I tried everything such as restarting Postgres service, different users/passwords, deleting container/image, uninstall/install Postgres, uninstall/install Docker and nothing worked! Any pointer would be greatly appreciated!

$ pgcli -h localhost -p 5432 -u root -d ny_taxi
Password for root: 
connection to server at "localhost" (::1), port 5432 failed: FATAL:  password authentication 
failed for user "root"
docker run -it \
-e POSTGRES_USER="root" \
-e POSTGRES_PASSWORD="root" \
-e POSTGRES_DB="ny_taxi" \
-v c:/Users/lyesd/Desktop/DE-ZoomCamp/week1_basics_n_setup/2_docker_sql/ny_taxi_postgres_data:/var/lib/postgresql/data \
-p 5431:5432 \
postgres:13
$ docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                    NAMES
7750be3fadfb   postgres:13   "docker-entrypoint.s…"   18 minutes ago   Up 17 minutes   0.0.0.0:5431->5432/tcp   gallant_agnesi
lyesd@LAPTOP-KB2SKS8F MINGW64 ~/Desktop/DE-ZoomCamp/week1_basics_n_setup/2_docker_sql
$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
b371e0426e03   bridge    bridge    local
cc4456f5fb6a   host      host      local
263d85d9a166   none      null      local
$ docker container ls
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                    
NAMES
7750be3fadfb   postgres:13   "docker-entrypoint.s…"   29 minutes ago   Up 29 minutes   
0.0.0.0:5431->5432/tcp   gallant_agnesi

Hi

According to your “docker container ls”
you have made the following mapping:

0.0.0.0:5431->5432/tcp

This means that your LOCAL port 5431 will be mapped to the container port 5432

So you should connect to port 5431 and not 5432 (as you did in your first codeblock)

1 Like

@terpz I can’t believe it was only the port, you saved me the little sanity I had left after fighting with this issue for the last 48 hours! You are the man!

1 Like

(duplicate content deleted by rimelek)

connection to server at "localhost" (::1), port 5432 failed: FATAL:  password au
thentication failed for user "root

pls am having this problem .

$ docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS
       PORTS                    NAMES
552f1016dce6   postgres:13   "docker-entrypoint.s…"   52 minutes ago   Up 52 min
utes   0.0.0.0:5432->5432/tcp   intelligent_lalande
$  pgcli -h localhost -p 5432 -u root -d ny_taxi
Password for root:
connection to server at "localhost" (::1), port 5432 failed: FATAL:  password au
thentication failed for user "root

I edited your first post to delete the content that you repeated in the second post. Make sure you edit your post instead of sending a new repeating the content of the first.
I also edited your second post. Please, use code blocks (</> button) when you insert codes or terminal outputs.

Can you authenticate from the postgresql container instead of the host? Are you sure you used the right password? How did you created that postgres container?