# Cannot connect to the postgresql database using mattermost cli

**URL:** <https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809>\
**Category:** Troubleshooting\
**Created:** [August 1, 2019, 7:36am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809 "2019-08-01T07:36:57Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinci](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@vinci](https://forum.mattermost.com/u/vinci)\
**Post date:** [August 1, 2019, 7:36am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/1 "2019-08-01T07:36:57Z")

</div>

#### Summary

Unable to connect to postgresql database lying on a different container than the database

#### Steps to reproduce

I’m using the mattermost command.  
mattermost user create --email [test2@email.com](mailto:test2@email.com) --username testatene --password Password

#### Observed behavior

{“level”:“error”,“ts”:1564644335.8744648,“caller”:“sqlstore/supplier.go:236”,“msg”:“Failed to ping DB retrying in 10 seconds err=dial tcp 127.0.0.1:5432: connect: connection refused”}

Is there any way I can change the postgresql path? The container is reacheable through the “db” name set in docker-compose, but I haven’t found a way to set that path.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ahmaddanial](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/ahmaddanial/32/2938_2.png) [@ahmaddanial](https://forum.mattermost.com/u/ahmaddanial)\
**Post date:** [August 5, 2019, 3:42am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/2 "2019-08-05T03:42:25Z")

</div>

Hello, @vinci.

Perhaps we can start by looking on the following:

- Did you deploy Mattermost through the [Production Docker Deployment](https://docs.mattermost.com/install/prod-docker.html)?

- Since you mentioned that the container is reachable through the database name, can you please let us know how did you confirm that? Did you try to `ping` to the machine hosting the database from the container and vice versa or did you use any other methods?

- Can you share a copy of the `docker-compose.yaml` here so we can go through it together?

---

<div class="post-metadata">

**Author:** ![vinci](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@vinci](https://forum.mattermost.com/u/vinci)\
**Post date:** [August 7, 2019, 7:10pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/3 "2019-08-07T19:10:17Z")

</div>

Hi,  
Yes, this is the docker-compose I’m using. It comes directly from the gitlab site ([https://github.com/mattermost/mattermost-docker](https://github.com/mattermost/mattermost-docker)). This seems to be defaul behaviour with mattermost. I’ve got two somewhat different installations with the same problem, one of which uses different settings for the reverse proxy:

```auto
version: "2"

services:

  db:
    build: db
    read_only: true
    restart: unless-stopped
    volumes:
      - ./volumes/db/var/lib/postgresql/data:/var/lib/postgresql/data
      - /etc/localtime:/etc/localtime:ro
    environment:
      - POSTGRES_USER=mmuser
      - POSTGRES_PASSWORD=mmuser_password
      - POSTGRES_DB=mattermost
    # uncomment the following to enable backup
    # - AWS_ACCESS_KEY_ID=XXXX
    # - AWS_SECRET_ACCESS_KEY=XXXX
    # - WALE_S3_PREFIX=s3://BUCKET_NAME/PATH
    # - AWS_REGION=us-east-1

  app:
    build:
      context: app
      # uncomment following lines for team edition or change UID/GID
      args:
        - edition=team
        - PUID=5000
        - PGID=5000
    restart: unless-stopped
    volumes:
      - config:/mattermost/config:rw
      - data:/mattermost/data:rw
      - logs:/mattermost/logs:rw
      - plugins:/mattermost/plugins:rw
      - client-plugins:/mattermost/client/plugins:rw
      - /etc/localtime:/etc/localtime:ro
    environment:
      # set same as db credentials and dbname
      - MM_USERNAME=mmuser
      - MM_PASSWORD=mmuser_password
      - MM_DBNAME=mattermost
      # in case your config is not in default location
      #- MM_CONFIG=/mattermost/config/config.json
    extra_hosts:
      - "mm.vinci.de:192.168.99.12"
      - "git.vinci.de:192.168.99.11"

  web:
    build: web
    ports:
      - "80:80"
      - "443:443"
    read_only: true
    restart: unless-stopped
    volumes:
      # This directory must have cert files if you want to enable SSL
      - ./volumes/web/cert:/cert:ro
      - /etc/localtime:/etc/localtime:ro
    # Uncomment for SSL
    # environment:
    # - MATTERMOST_ENABLE_SSL=true
    extra_hosts:
      - "mm.vinci.de:192.168.99.12"
      - "git.vinci.de:192.168.99.11"
volumes:
    config:
    data:
    logs:
    plugins:
    client-plugins:

```

So as you see, the images were built directly using the github files of the mattermost official repo.  
I can’t ping into the other container, I’m guessing it’s because I can’t do with with the mattermost user (probably because it’s not allowed access to ping etc.) Ping does work from database to the app, though.  
How would the application know to reach the ‘db’ name nonetheless? It can’t work like that, if it’s not set somewhere in the configuration files or whatever. I could have named the container something else, but I’d still expect it to work by being able to offer it the path to the db.

Later did I see that there’s actually a DB\_HOST=${DB\_HOST:-db} in the Dockerfile, which doesn’t actually appear in the environmental variables in the in the app container.

---

<div class="post-metadata">

**Author:** ![sven.huester](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/sven.huester/32/8724_2.png) [@sven.huester](https://forum.mattermost.com/u/sven.huester)\
**Post date:** [August 12, 2019, 1:48pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/4 "2019-08-12T13:48:21Z")

</div>

Hey @vinci,  
what is the exact command or commands you use to run this?

I’ve found that running the command from inside the machine did not work.  
However using docker exec worked for me because then all the environment vars are set correctly.  
So for example to get the server version I’d use  
`docker exec -it mattermost_docker_app_1 /mattermost/bin/mattermost version`

---

<div class="post-metadata">

**Author:** ![vinci](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@vinci](https://forum.mattermost.com/u/vinci)\
**Post date:** [August 29, 2019, 3:30pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/5 "2019-08-29T15:30:30Z")

</div>

Hi, sven,

Thank you for the tip. Unfortunately it still doesn’t work. I would have been pretty surprised to work from the docker command, but not from inside the container, to be honest. The variables are there and the user should have access to them. This is what I’ve tried:

```auto
root@mattermost:/home/atkadmin# docker exec -it 9c12 sh
~ $ mattermost version
{"level":"error","ts":1567092520.6169271,"caller":"sqlstore/supplier.go:236","msg":"Failed to ping DB retrying in 10 seconds err=dial tcp 127.0.0.1:5432: connect: connection refused"}
^C
~ $ root@mattermost:/home/atkadmin# docker exec -it 9c12 mattermost version
{"level":"error","ts":1567092530.4008448,"caller":"sqlstore/supplier.go:236","msg":"Failed to ping DB retrying in 10 seconds err=dial tcp 127.0.0.1:5432: connect: connection refused"}

```

Any other ideas? This is very annoying 🙂

---

<div class="post-metadata">

**Author:** ![retsofaj](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@retsofaj](https://forum.mattermost.com/u/retsofaj)\
**Post date:** [August 31, 2019, 6:59pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/6 "2019-08-31T18:59:23Z")

</div>

I can confirm that a fresh install as of a few days ago exhibits the same behaviour. This happens both with `docker-compose exec app mattermost` and `docker-compose exec app sh` followed by `/mattermost/bin/mattermost`

Running `docker-compose exec app sh` and then `env` results in:

```
MM_PASSWORD=mmuser_password
MM_VERSION=5.13.2
HOSTNAME=cd3a95bea51a
SHLVL=1
HOME=/mattermost
TERM=xterm
PATH=/mattermost/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MM_USERNAME=mmuser
MM_DBNAME=mattermost
PWD=/mattermost

```

Mattermost is working fine through the web app and through the macOS client - it’s only the command line which seems to be broken.

---

<div class="post-metadata">

**Author:** ![vinci](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@vinci](https://forum.mattermost.com/u/vinci)\
**Post date:** [September 7, 2019, 7:49pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/7 "2019-09-07T19:49:14Z")

</div>

Thank you for confirming the issue.  
For me it would be really important to be able to use the command, as there are several things that you can do only with the command line, such as completely removing users.

---

<div class="post-metadata">

**Author:** ![retsofaj](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@retsofaj](https://forum.mattermost.com/u/retsofaj)\
**Post date:** [September 7, 2019, 11:47pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/8 "2019-09-07T23:47:29Z")

</div>

I’ve gone one step further and installed from Docker and then removed all references to external volumes so that it’s a completely isolated installation. And the same issue persists.

I’m hoping to add 300+ user accounts which is another thing that the command line enables. I’m really curious what the underlying issue is?

---

<div class="post-metadata">

**Author:** ![vinci](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@vinci](https://forum.mattermost.com/u/vinci)\
**Post date:** [September 10, 2019, 8:09pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/9 "2019-09-10T20:09:11Z")

</div>

I can’t see it as anything else than negligence. It’s obvious that this works if you install it conventionally outside docker, as it would connect to the localhost, which inside a VM would be common to all services, so there would be a problem in the first place.  
[later edit:]  
So this is what I’ve done to try accessing the database.  
First of all I deleted the USER mattermost directive from the dockerfile of the application container, so that I can start the container with root privileges. Then I’ve installed socat and ran:

```auto
socat tcp-listen:5432,reuseaddr,fork tcp:172.19.0.2:5432

```

172.19.0.2 being the database container.  
What do you think I get?🙂

```auto
{"level":"error","ts":1568147811.9297912,"caller":"sqlstore/supplier.go:235","msg":"Failed to ping DB retrying in 10 seconds err=pq: SSL is not enabled on the server"}

```

It’s as if it never even crossed their minds to establish the connection between the containers using the command. I still don’t understand though why the application is able to connect to the database, as mattermost works fine otherwise.

[even later on:]  
I’ve set up ssl on the database, just for the sake of it, then I switched to the mattermost user using su -, but for some reason the mattermost binary wasn’t found in the path. So I ran:

```auto
/mattermost/bin/mattermost version
{"level":"error","ts":1568149541.235386,"caller":"sqlstore/supplier.go:235","msg":"Failed to ping DB retrying in 10 seconds err=pq: password authentication failed for user \"mattermost\""}

```

---

<div class="post-metadata">

**Author:** ![Ansty93](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@Ansty93](https://forum.mattermost.com/u/Ansty93)\
**Post date:** [September 12, 2019, 11:27am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/10 "2019-09-12T11:27:00Z")

</div>

Any update on this?  
Iam struggling with the same problem! The mattermost instance is up and running with docker, no problem at all. But i cant get access to the command line interface, iam getting same error **Connection Refused**

```auto
andreas@filesafe:/filesafe/prolikematters$ docker exec -it prolikematters_app_1 /mattermost/bin/mattermost version
{"level":"error","ts":1568287324.1087985,"caller":"sqlstore/supplier.go:235","msg":"Failed to ping DB retrying in 10 seconds err=dial tcp 127.0.0.1:5432: connect: connection refused"}
{"level":"error","ts":1568287334.110559,"caller":"sqlstore/supplier.go:235","msg":"Failed to ping DB retrying in 10 seconds err=dial tcp 127.0.0.1:5432: connect: connection refused"}
{"level":"error","ts":1568287344.1120512,"caller":"sqlstore/supplier.go:235","msg":"Failed to ping DB retrying in 10 seconds err=dial tcp 127.0.0.1:5432: connect: connection refused"}

```

I have followed the mattermost production deployment guideline, this server have nginx running as reverse proxy, and letsencrypt for ssl certification. But i guess there is not these which cause the problem?

My docker-compose yml looks like:

```auto
version: "2"

services:
  db:
    build: db
    read_only: true
    restart: unless-stopped
    volumes:
      - ./volumes/db/var/lib/postgresql/data:/var/lib/postgresql/data
      - /etc/localtime:/etc/localtime:ro
    environment:
      - POSTGRES_USER=mmuser
      - POSTGRES_PASSWORD=mmuser_password
      - POSTGRES_DB=mattermost
    # uncomment the following to enable backup
    # - AWS_ACCESS_KEY_ID=XXXX
    # - AWS_SECRET_ACCESS_KEY=XXXX
    # - WALE_S3_PREFIX=s3://BUCKET_NAME/PATH
    # - AWS_REGION=us-east-1

  app:
    build:
      context: app
      # uncomment following lines for team edition or change UID/GID
      args:
       - edition=team
      # - PUID=1000
      # - PGID=1000
    restart: unless-stopped
    volumes:
      - ./volumes/app/mattermost/config:/mattermost/config:rw
      - ./volumes/app/mattermost/data:/mattermost/data:rw
      - ./volumes/app/mattermost/logs:/mattermost/logs:rw
      - ./volumes/app/mattermost/plugins:/mattermost/plugins:rw
      - ./volumes/app/mattermost/client-plugins:/mattermost/client/plugins:rw
      - /etc/localtime:/etc/localtime:ro
    environment:
      # set same as db credentials and dbname
      - MM_USERNAME=mmuser
      - MM_PASSWORD=mmuser_password
      - MM_DBNAME=mattermost
      - VIRTUAL_HOST=matters.prolike.io
      - LETSENCRYPT_HOST=matters.prolike.io
      # in case your config is not in default location
      #- MM_CONFIG=/mattermost/config/config.json

networks:
  default:
    external:
      name: webproxy

```

---

<div class="post-metadata">

**Author:** ![retsofaj](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@retsofaj](https://forum.mattermost.com/u/retsofaj)\
**Post date:** [September 16, 2019, 2:26am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/11 "2019-09-16T02:26:32Z")

</div>

Thanks to the magic of `git checkout` and way too much time spent, I can confirm that the breakage took place between `mattermost-docker 5.11.1` and `mattermost-docker 5.12.0`

> docker exec mattermostdocker\_app\_1 sh -c ‘/mattermost/bin/mattermost team create --name test --display\_name “test”’

… works fine on `5.11.1` whereas the **exact** same `docker-compose.yml` (and associated filesystem mount points) results in the database error on `5.12.0`

There were 260 commits between those tags, and I’m hoping that someone with more knowledge than I of Mattermost’s internals can quickly spot what caused the breakage.

---

<div class="post-metadata">

**Author:** ![Ansty93](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@Ansty93](https://forum.mattermost.com/u/Ansty93)\
**Post date:** [September 17, 2019, 9:15am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/12 "2019-09-17T09:15:13Z")

</div>

Excellent investigation!

Now we just have to wait for the mattermost team to review this issue and hopefully somewhen they are able to resolve it.

---

<div class="post-metadata">

**Author:** ![roemil](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@roemil](https://forum.mattermost.com/u/roemil)\
**Post date:** [September 24, 2019, 11:03am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/13 "2019-09-24T11:03:21Z")

</div>

I have the same issues on mattermost 5.15.0. Very interested to have this fixed asap.

---

<div class="post-metadata">

**Author:** ![vinci](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@vinci](https://forum.mattermost.com/u/vinci)\
**Post date:** [September 29, 2019, 4:13pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/14 "2019-09-29T16:13:13Z")

</div>

I opened this thread two months ago and the problem started on the 15 June, and it’s still being ignored…  
They are very careful about not supporting direct ldap authentication themselves, so that you buy the commercial version (I’m ok with that in itself), but they have a huge problem for such a long time that they’re not addressing. Not really nice. How can this be considered enterprise ready?

---

<div class="post-metadata">

**Author:** ![Ansty93](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@Ansty93](https://forum.mattermost.com/u/Ansty93)\
**Post date:** [September 30, 2019, 7:14am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/15 "2019-09-30T07:14:13Z")

</div>

Yes this is not really nice, we need this issue fixed asap.  
Could we somehow notify/ping the dev-team

---

<div class="post-metadata">

**Author:** ![Ansty93](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@Ansty93](https://forum.mattermost.com/u/Ansty93)\
**Post date:** [September 30, 2019, 7:19am UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/16 "2019-09-30T07:19:02Z")

</div>

Well it looks like the issue is open in [https://github.com/mattermost/mattermost-server/issues/12327](https://github.com/mattermost/mattermost-server/issues/12327)  
and it is now registered in mattermost issue-tracker system: [https://mattermost.atlassian.net/browse/MM-18794](https://mattermost.atlassian.net/browse/MM-18794)

So i guess they are working on it

---

<div class="post-metadata">

**Author:** ![vinci](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@vinci](https://forum.mattermost.com/u/vinci)\
**Post date:** [November 5, 2019, 7:44pm UTC](https://forum.mattermost.com/t/cannot-connect-to-the-postgresql-database-using-mattermost-cli/7809/17 "2019-11-05T19:44:32Z")

</div>

Three-month’s problem and still not solved, although it’s an essential tool for managing mattermost… ffs…
