# \[Solved\] Docker setup: mattermoset tries to connect to wrong postgres container

**URL:** <https://forum.mattermost.com/t/solved-docker-setup-mattermoset-tries-to-connect-to-wrong-postgres-container/17048>\
**Category:** Troubleshooting\
**Created:** [October 9, 2023, 3:05pm UTC](https://forum.mattermost.com/t/solved-docker-setup-mattermoset-tries-to-connect-to-wrong-postgres-container/17048 "2023-10-09T15:05:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arminus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/arminus/32/7264_2.png) [@arminus](https://forum.mattermost.com/u/arminus)\
**Post date:** [October 9, 2023, 3:05pm UTC](https://forum.mattermost.com/t/solved-docker-setup-mattermoset-tries-to-connect-to-wrong-postgres-container/17048/1 "2023-10-09T15:05:55Z")

</div>

**Summary**  
Mattermost docker config does not seem to properly handle MM\_SQLSETTINGS\_DATASOURCE when pointing to a container/hist other than “postgres”

**Steps to reproduce**  
On the host I’m trying to deploy mattermost, I already have an existing postgres container running (named “postgres”) When I first spun up mattermost, it tried and failed to connect to that container - i.e. in the logs, I saw that it used the docker network IP of that pre-existing postgres container.

So I saw [this](https://forum.mattermost.com/t/docker-with-separate-postgresql/15597) and modified my setup accordingly:

.env:  
`MM_SQLSETTINGS_DATASOURCE=postgres_mattermost://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable&connect_timeout=10`

docker-compose.yml:

```auto
services:
  postgres_mattermost:
    image: postgres:${POSTGRES_IMAGE_TAG}
...
  mattermost:
    depends_on:
      - postgres_mattermost
...
    environment:
      - MM_SQLSETTINGS_DATASOURCE

```

(I also deleted all pertinent volume dirs from previous starts)

What I see in the mattermost container log now though is this:

```auto
   {"timestamp":"2023-10-09 17:02:59.530 +02:00","level":"error","msg":"Failed to ping DB","caller":"sqlstore/store.go:252","error":"dial │
│ tcp 127.0.0.1:5432: connect: connection refused","retrying in seconds":10} 

```

127.0.0.1:5432 is obviously wrong.

**Expected behavior**  
With MM\_SQLSETTINGS\_DATASOURCE set as above, I’d excpect it to connect to my postgres\_mattermost container.

**Observed behavior**  
See above

---

<div class="post-metadata">

**Author:** ![arminus](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/arminus/32/7264_2.png) [@arminus](https://forum.mattermost.com/u/arminus)\
**Post date:** [October 10, 2023, 6:40am UTC](https://forum.mattermost.com/t/solved-docker-setup-mattermoset-tries-to-connect-to-wrong-postgres-container/17048/2 "2023-10-10T06:40:24Z")

</div>

Ok, I’m obviously stupid, the proper `MM_SQLSETTINGS_DATASOURCE` is `postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres_mattermost:5432/${POSTGRES_DB}?sslmode=disable&connect_timeout=10`  
when pointing to a container named postgres\_mattermost

Case closed

---

<div class="post-metadata">

**Author:** ![john.combs](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/john.combs/32/7118_2.png) [@john.combs](https://forum.mattermost.com/u/john.combs)\
**Post date:** [October 17, 2023, 8:26pm UTC](https://forum.mattermost.com/t/solved-docker-setup-mattermoset-tries-to-connect-to-wrong-postgres-container/17048/3 "2023-10-17T20:26:38Z")

</div>

Woot! Thanks so much for following up to let us know, Arminus!
