# Can't connect to Postgres through Docker

**URL:** <https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051>\
**Category:** Docker Desktop\
**Tags:** windows\
**Created:** [January 19, 2022, 10:50pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051 "2022-01-19T22:50:56Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyesd](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lyesd](https://forums.docker.com/u/lyesd)\
**Post date:** [January 19, 2022, 10:50pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/1 "2022-01-19T22:50:56Z")

</div>

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!

```auto
$ 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"

```

```auto
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

```

```auto
$ 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

```

```auto
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

```

```auto
$ 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

```

---

<div class="post-metadata">

**Author:** ![terpz](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/terpz/32/36125_2.png) [@terpz](https://forums.docker.com/u/terpz)\
**Post date:** [January 20, 2022, 7:54am UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/2 "2022-01-20T07:54:33Z")

</div>

Hi

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

```auto
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)

---

<div class="post-metadata">

**Author:** ![lyesd](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lyesd](https://forums.docker.com/u/lyesd)\
**Post date:** [January 20, 2022, 2:34pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/3 "2022-01-20T14:34:45Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![funsorock](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/funsorock/32/32598_2.png) [@funsorock](https://forums.docker.com/u/funsorock)\
**Post date:** [May 28, 2022, 8:50pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/4 "2022-05-28T20:50:55Z")

</div>

(duplicate content deleted by rimelek)

---

<div class="post-metadata">

**Author:** ![funsorock](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/funsorock/32/32598_2.png) [@funsorock](https://forums.docker.com/u/funsorock)\
**Post date:** [May 28, 2022, 8:52pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/5 "2022-05-28T20:52:30Z")

</div>

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

```

pls am having this problem .

```bash
$ 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

```

```bash
$ 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

```

---

<div class="post-metadata">

**Author:** ![rimelek](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rimelek/32/41602_2.png) [@rimelek](https://forums.docker.com/u/rimelek)\
**Post date:** [May 29, 2022, 6:05pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/6 "2022-05-29T18:05:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![khairah0319](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/khairah0319/32/39286_2.png) [@khairah0319](https://forums.docker.com/u/khairah0319)\
**Post date:** [February 14, 2023, 9:38am UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/7 "2023-02-14T09:38:13Z")

</div>

Hi, I’m having the same problem as above.

```auto
Error connecting to database
panic: failed to connect to `host=localhost user=test_admin database=api`: failed SASL auth (FATAL: password authentication failed for user "test_admin" (SQLSTATE 28P01))

```

```auto
C:\Users\User>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc1746744c31 postgres:10.14-alpine "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:5118->5118/tcp, 5432/tcp payd-exercise-postgres

```

I have changed the port from 5432 to 5433 and finally 5118. I thought it’s a port conflict as I have pgadmin4 running at port 5432 as well. But still, I can’t connect to database running from Docker.

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [February 14, 2023, 5:43pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/8 "2023-02-14T17:43:54Z")

</div>

Please share the exact command you used to create the postgres container.

It looks like you publish host port 5118 on container port 5118. Did you change the postgres port inside the container to 5118 as well? (this is not a suggestion to do so, actually please don’t!)

---

<div class="post-metadata">

**Author:** ![damianobarbati](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/damianobarbati/32/40019_2.png) [@damianobarbati](https://forums.docker.com/u/damianobarbati)\
**Post date:** [March 29, 2023, 11:16am UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/9 "2023-03-29T11:16:56Z")

</div>

@lyesd you simply need to use 127.0.0.1 as -h parameter.

When you use “localhost” pg/mysql clients are assuming to find the .sock file locally and use file based socket communication with the server: but the problem is the file is inside the container!

When you use “127.0.0.1” pg/mysql clients are instead using network for communication with the server, and it works fine.

---

<div class="post-metadata">

**Author:** ![ahmedmaher0](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/ahmedmaher0/32/47893_2.png) [@ahmedmaher0](https://forums.docker.com/u/ahmedmaher0)\
**Post date:** [May 19, 2024, 12:00am UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/10 "2024-05-19T00:00:52Z")

</div>

```auto
docker run --name postgres -p 5433:5432 -h 127.0.0.1 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:latest

```

I have changed the mapped port from 5432:5432 To 5433:5432 and now it’s working fine and connected

---

<div class="post-metadata">

**Author:** ![atj393](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/atj393/32/50829_2.png) [@atj393](https://forums.docker.com/u/atj393)\
**Post date:** [August 28, 2024, 6:54pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/11 "2024-08-28T18:54:05Z")

</div>

This worked. Thanks!

---

<div class="post-metadata">

**Author:** ![raoufbouzerara](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/raoufbouzerara/32/59139_2.png) [@raoufbouzerara](https://forums.docker.com/u/raoufbouzerara)\
**Post date:** [May 6, 2025, 8:31am UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/12 "2025-05-06T08:31:17Z")

</div>

Thanks, this worked fine, changing the mapped port fixed my problem

---

<div class="post-metadata">

**Author:** ![innoaal](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/innoaal/32/54541_2.png) [@innoaal](https://forums.docker.com/u/innoaal)\
**Post date:** [May 8, 2025, 10:21am UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/13 "2025-05-08T10:21:19Z")

</div>

**that worked for me as well** , in my case on macOS, I was getting SSL errors even if ssl was disabled.

---

<div class="post-metadata">

**Author:** ![erickgx](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/erickgx/32/60307_2.png) [@erickgx](https://forums.docker.com/u/erickgx)\
**Post date:** [July 6, 2025, 4:48pm UTC](https://forums.docker.com/t/cant-connect-to-postgres-through-docker/120051/14 "2025-07-06T16:48:24Z")

</div>

Brother, you saved my life. I literally spent the whole day trying to find the problem with my application not being able to find the database. I tried everything I could, but just changing the mapped port solved it. Dude, something as simple as that and I didn’t even think to test it, damn. You saved my life.
