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_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 \
$ docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                    NAMES
7750be3fadfb   postgres:13   "docker-entrypoint.s…"   18 minutes ago   Up 17 minutes>5432/tcp   gallant_agnesi
lyesd@LAPTOP-KB2SKS8F MINGW64 ~/Desktop/DE-ZoomCamp/week1_basics_n_setup/2_docker_sql
$ docker network ls
b371e0426e03   bridge    bridge    local
cc4456f5fb6a   host      host      local
263d85d9a166   none      null      local
$ docker container ls
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                    
7750be3fadfb   postgres:13   "docker-entrypoint.s…"   29 minutes ago   Up 29 minutes>5432/tcp   gallant_agnesi


According to your “docker container ls”
you have made the following mapping:>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