Postgres connection refused

I’ve deployed the chirpstack services + postgres with docker compose. The containers don’t have any problems when they have to connect to other containers like mosquitto. I’m trying to add the postgres integration, so I edited its toml file:

[application_server.integration]
enabled=["mqtt", "postgresql"]

[postgresql]
dsn="postgres://chirpstack_as:chirpstack_as@postgresql/chirpstack_as?sslmode=disable"

I’ve also exposed the port in the docker-compose file:

version: "3"

services:
  chirpstack-network-server:
    image: chirpstack/chirpstack-network-server:3
    volumes:
      - ./configuration/chirpstack-network-server:/etc/chirpstack-network-server

  chirpstack-application-server:
    image: chirpstack/chirpstack-application-server:3
    ports:
      - 8081:8080
    volumes:
      - ./configuration/chirpstack-application-server:/etc/chirpstack-application-server

  chirpstack-gateway-bridge:
    image: chirpstack/chirpstack-gateway-bridge:3
    ports:
      - 1700:1700/udp
    volumes:
      - ./configuration/chirpstack-gateway-bridge:/etc/chirpstack-gateway-bridge

  chirpstack-geolocation-server:
    image: chirpstack/chirpstack-geolocation-server:3
    volumes:
      - ./configuration/chirpstack-geolocation-server:/etc/chirpstack-geolocation-server

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

  redis:
    image: redis:5-alpine
    volumes:
      - redisdata:/data

  mosquitto:
    image: eclipse-mosquitto
    ports:
      - 1883:1883
    volumes:
      - ./configuration/mosquitto:/mosquitto/config/
      - ./configuration/mosquitto/logs:/var/log/mosquitto

volumes:
  postgresqldata:
  redisdata:

However the chirpstack application doesn’t seem to connect with the postgres container. From its logs:

time="2021-11-30T11:05:30.238296643Z" level=info msg="starting ChirpStack Application Server" docs="https://www.chirpstack.io/" version=
time="2021-11-30T11:05:30.238396612Z" level=info msg="storage: setting up storage package"
time="2021-11-30T11:05:30.238428593Z" level=info msg="storage: setup metrics"
time="2021-11-30T11:05:30.238479373Z" level=info msg="storage: setting up Redis client"
time="2021-11-30T11:05:30.238628981Z" level=info msg="storage: connecting to PostgreSQL database"
time="2021-11-30T11:05:30.272370748Z" level=info msg="storage: applying PostgreSQL data migrations"
time="2021-11-30T11:05:30.301162017Z" level=info msg="integration: configuring global integrations"
time="2021-11-30T11:05:30.301741884Z" level=info msg="integration/mqtt: TLS config is empty"
time="2021-11-30T11:05:30.301789365Z" level=info msg="integration/mqtt: connecting to mqtt broker" server="tcp://mosquitto:1883"
time="2021-11-30T11:05:30.304668978Z" level=info msg="integration/postgresql: connecting to PostgreSQL database"
time="2021-11-30T11:05:30.304760821Z" level=info msg="integration/mqtt: connected to mqtt broker"
time="2021-11-30T11:05:30.304926557Z" level=info msg="integration/mqtt: subscribing to tx topic" qos=0 topic=application/+/device/+/command/down
time="2021-11-30T11:05:30.306507168Z" level=warning msg="integration/postgresql: ping PostgreSQL database error, will retry in 2s" error="dial tcp 127.0.0.1:5432: connect: connection refused"
time="2021-11-30T11:05:32.308815371Z" level=warning msg="integration/postgresql: ping PostgreSQL database error, will retry in 2s" error="dial tcp 127.0.0.1:5432: connect: connection refused"

And goes on with the connection refused message. It seems it’s trying to connect to a local instance rather than the one defined in the toml.

I’ve tested the postgres container and I can:

  • connect to the database from the local machine
  • connect to the database from a remote machine
  • connect to the database from another container, I’ve used jbergknoff/postgresql-client with the same dns string.

All with the same username / password / database.

@LowRez

Follow below url document for Postgresql integration

You need to create separate database (chirpstack_as_events) for postgresql integration.

https://www.chirpstack.io/application-server/integrations/postgresql/

Thanks for the answer @sagarpatel. I have already followed it.

You need to create separate database (chirpstack_as_events ) for postgresql integration as per the above documentation steps

I’ve set up a new DB called just like in the example and changed the dns attributes, but it’s still not working. In my previous configuration I had already created and I called it differently “chirpstack_as” and I’ve used the same name in the dns configuration.

Found the problem. In the config file you need BOTH configs for postgres, so from mine it would be:

[application_server.integration]
enabled=[“mqtt”, “postgresql”]

[application_server.integration.postgresql]
dsn=“postgres://chirpstack_as_events:dbpassword@postgresql/chirpstack_as_events?sslmode=disable”

[postgresql]
dsn=“postgres://chirpstack_as:chirpstack_as@postgresql/chirpstack_as?sslmode=disable”

Totally my fault. Thanks for the help @sagarpatel

1 Like