[Docker Compose] Connect to postgresql failure


#1

I cloned loraserver-docker from github and use docker-compose up to deploy and startup loraserver.

log shows postgresql is started up:
appserver_1 | time=“2019-06-11T08:58:53Z” level=info msg=“storage: applying PostgreSQL data migrations”
loraserver_1 | time=“2019-06-11T08:58:53Z” level=info msg=“storage: applying PostgreSQL data migrations”
loraserver_1 | time=“2019-06-11T08:58:55Z” level=info msg=“storage: PostgreSQL data migrations applied” count=21
loraserver_1 | time=“2019-06-11T08:58:55Z” level=info msg=“gateway/mqtt: connecting to mqtt broker” server=“tcp://mosquitto:1883”

When I try ‘psql -h localhost -U loraserver_ns’ or ‘psql -h localhost -U postgres’ , there is error as follow:
psql: could not connect to server: Connection refused
Is the server running on host “localhost” (127.0.0.1) and accepting
TCP/IP connections on port 5432?


#2

By default the PostgreSQL Docker container does not expose its ports. See for example how this was done for Mosquitto: https://github.com/brocaar/loraserver-docker/blob/master/docker-compose.yml#L41. You would need to do the same for the PostgreSQL container to make it accessible from the outside.


#3

Hi brocaar,
Thanks for your reply!
I updated docker-compose.yml file as follow:

postgresql:
image: postgres:9.6-alpine
ports:
- 5432:5432
volumes:
- ./configuration/postgresql/initdb:/docker-entrypoint-initdb.d
- postgresqldata:/var/lib/postgresql/data

But still can not get access to postgresql.

postgresql is running.

edge@edge:~/loraserver-docker$ sudo netstat -tunlp |grep 5432
tcp6 0 0 :::5432 :::* LISTEN 17098/docker-proxy

edge@edge:~/loraserver-docker$ systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2019-06-13 16:29:17 CST; 1h 7min ago
Process: 1360 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 1360 (code=exited, status=0/SUCCESS)

Please let me know if I missed something

Thanks,
Bruce


#4

edge@edge:~/loraserver-docker$ systemctl status postgresql

This controls the PostgreSQL instance outside the Docker container, not the PostgreSQL instance inside the container, so this does not work. You could however use the psql cli tool to connect to the exposed port.