Problem while automating chirpstack installation

Hello, I need to automate the installation of chirpstack using a bash script. But I got a problem between the application server and the postgresql database. I tried to ues a previous version of postgres, but it was useless. I also tried to do my script, step by step, writing commands in the shell. But it still doesn’t works. Anyone has an idea?

Here is my script:

#!/bin/bash
echo -e "\e[1m\e[34m##Installing mosquitto, mosquitto-clients, redis-server, redis-tools and postgresql##\e[0m"
sudo apt install -y mosquitto mosquitto-clients redis-server redis-tools make postgresql &> /dev/null
echo -e "\e[1m\e[34m##########Setting up postgresql##########\e[0m"
sudo cp res/postgres/initdb.sql /usr/share
sudo chmod 777 /usr/share/initdb.sql
sudo -H -u postgres psql -f /usr/share/initdb.sql &> /dev/null
echo -e "\e[1m\e[34m##########Setting up mosquitto##########\e[0m"
sudo systemctl stop mosquitto.service
sudo cp res/mosquitto/* /etc/mosquitto
sudo mosquitto_passwd -b -c /etc/mosquitto/passwd admin PASSWORD
sudo systemctl start mosquitto.service
echo -e "\e[1m\e[34m##########Installing chirpstack##########\e[0m"
sudo dpkg -i res/chirpstack/pkg/* &> /dev/null
echo -e "\e[1m\e[34m##########Setting up chirpstack##########\e[0m"
sudo cp res/chirpstack/chirpstack-application-server.toml /etc/chirpstack-application-server/
sudo cp res/chirpstack/chirpstack-network-server.toml /etc/chirpstack-network-server/
sudo cp res/chirpstack/chirpstack-gateway-bridge.toml /etc/chirpstack-gateway-bridge/
echo -e "\e[1m\e[34m##########Starting chirpstack##########\e[0m"
sudo systemctl start chirpstack-gateway-bridge
sudo systemctl enable chirpstack-gateway-bridge
sudo systemctl start chirpstack-network-server
sudo systemctl enable chirpstack-network-server
sudo systemctl start chirpstack-application-server
sudo systemctl enable chirpstack-application-server

Here is initdb.sql:

create role chirpstack_ns with login password 'PASSWORD';
create database chirpstack_ns with owner chirpstack_ns;
create role chirpstack_as with login password 'PASSWORD';
create database chirpstack_as with owner chirpstack_as;
create role admin with login password 'PASSWORD';
create database vna with owner admin;
\c chirpstack_as;
create extension pg_trgm;
create extension hstore;

Here is the error I get:

test@aled:~/ClientInstall$ sudo journalctl -f -n 100 -u chirpstack-application-server
May 17 13:26:30 aled systemd[1]: Started ChirpStack Application Server.
May 17 13:26:31 aled chirpstack-application-server[4773]: time="2022-05-17T13:26:31.121129005Z" level=info msg="starting ChirpStack Application Server" docs="https://www.chirpstack.io/" version=3.17.6
May 17 13:26:31 aled chirpstack-application-server[4773]: time="2022-05-17T13:26:31.144749536Z" level=info msg="storage: setting up storage package"
May 17 13:26:31 aled chirpstack-application-server[4773]: time="2022-05-17T13:26:31.145431069Z" level=info msg="storage: setup metrics"
May 17 13:26:31 aled chirpstack-application-server[4773]: time="2022-05-17T13:26:31.146065936Z" level=info msg="storage: setting up Redis client"
May 17 13:26:31 aled chirpstack-application-server[4773]: time="2022-05-17T13:26:31.146563428Z" level=info msg="storage: connecting to PostgreSQL database"
May 17 13:26:31 aled chirpstack-application-server[4773]: time="2022-05-17T13:26:31.217335909Z" level=info msg="storage: applying PostgreSQL data migrations"
May 17 13:26:33 aled chirpstack-application-server[4773]: time="2022-05-17T13:26:33.679534445Z" level=info msg="storage: PostgreSQL data migrations applied" from_version=0 to_version=60
May 17 13:26:33 aled chirpstack-application-server[4773]: time="2022-05-17T13:26:33.76486397Z" level=fatal msg="get keys error: dial tcp: lookup redis on 127.0.0.53:53: server misbehaving"
May 17 13:26:33 aled systemd[1]: chirpstack-application-server.service: Main process exited, code=exited, status=1/FAILURE
May 17 13:26:33 aled systemd[1]: chirpstack-application-server.service: Failed with result 'exit-code'.
May 17 13:26:34 aled systemd[1]: chirpstack-application-server.service: Scheduled restart job, restart counter is at 1.
May 17 13:26:34 aled systemd[1]: Stopped ChirpStack Application Server.
May 17 13:26:34 aled systemd[1]: Started ChirpStack Application Server.
May 17 13:26:34 aled chirpstack-application-server[4826]: time="2022-05-17T13:26:34.085020381Z" level=info msg="starting ChirpStack Application Server" docs="https://www.chirpstack.io/" version=3.17.6
May 17 13:26:34 aled chirpstack-application-server[4826]: time="2022-05-17T13:26:34.085710816Z" level=info msg="storage: setting up storage package"
May 17 13:26:34 aled chirpstack-application-server[4826]: time="2022-05-17T13:26:34.085951935Z" level=info msg="storage: setup metrics"
May 17 13:26:34 aled chirpstack-application-server[4826]: time="2022-05-17T13:26:34.086159291Z" level=info msg="storage: setting up Redis client"
May 17 13:26:34 aled chirpstack-application-server[4826]: time="2022-05-17T13:26:34.086387836Z" level=info msg="storage: connecting to PostgreSQL database"
May 17 13:26:34 aled chirpstack-application-server[4826]: time="2022-05-17T13:26:34.100773123Z" level=info msg="storage: applying PostgreSQL data migrations"
May 17 13:26:34 aled chirpstack-application-server[4826]: time="2022-05-17T13:26:34.185472253Z" level=fatal msg="get keys error: dial tcp: lookup redis on 127.0.0.53:53: server misbehaving"
May 17 13:26:34 aled systemd[1]: chirpstack-application-server.service: Main process exited, code=exited, status=1/FAILURE
May 17 13:26:34 aled systemd[1]: chirpstack-application-server.service: Failed with result 'exit-code'.
May 17 13:26:34 aled systemd[1]: chirpstack-application-server.service: Scheduled restart job, restart counter is at 2.
May 17 13:26:34 aled systemd[1]: Stopped ChirpStack Application Server.
May 17 13:26:34 aled systemd[1]: Started ChirpStack Application Server.
May 17 13:26:34 aled chirpstack-application-server[4832]: time="2022-05-17T13:26:34.582555941Z" level=info msg="starting ChirpStack Application Server" docs="https://www.chirpstack.io/" version=3.17.6
May 17 13:26:34 aled chirpstack-application-server[4832]: time="2022-05-17T13:26:34.583187206Z" level=info msg="storage: setting up storage package"
May 17 13:26:34 aled chirpstack-application-server[4832]: time="2022-05-17T13:26:34.583398188Z" level=info msg="storage: setup metrics"
May 17 13:26:34 aled chirpstack-application-server[4832]: time="2022-05-17T13:26:34.583591278Z" level=info msg="storage: setting up Redis client"
May 17 13:26:34 aled chirpstack-application-server[4832]: time="2022-05-17T13:26:34.583791691Z" level=info msg="storage: connecting to PostgreSQL database"
May 17 13:26:34 aled chirpstack-application-server[4832]: time="2022-05-17T13:26:34.597204152Z" level=info msg="storage: applying PostgreSQL data migrations"
May 17 13:26:34 aled chirpstack-application-server[4832]: time="2022-05-17T13:26:34.683200363Z" level=fatal msg="get keys error: dial tcp: lookup redis on 127.0.0.53:53: server misbehaving"
May 17 13:26:34 aled systemd[1]: chirpstack-application-server.service: Main process exited, code=exited, status=1/FAILURE
May 17 13:26:34 aled systemd[1]: chirpstack-application-server.service: Failed with result 'exit-code'.
May 17 13:26:35 aled systemd[1]: chirpstack-application-server.service: Scheduled restart job, restart counter is at 3.
May 17 13:26:35 aled systemd[1]: Stopped ChirpStack Application Server.
May 17 13:26:35 aled systemd[1]: Started ChirpStack Application Server.
May 17 13:26:35 aled chirpstack-application-server[4838]: time="2022-05-17T13:26:35.090626346Z" level=info msg="starting ChirpStack Application Server" docs="https://www.chirpstack.io/" version=3.17.6
May 17 13:26:35 aled chirpstack-application-server[4838]: time="2022-05-17T13:26:35.091282108Z" level=info msg="storage: setting up storage package"
May 17 13:26:35 aled chirpstack-application-server[4838]: time="2022-05-17T13:26:35.091509141Z" level=info msg="storage: setup metrics"
May 17 13:26:35 aled chirpstack-application-server[4838]: time="2022-05-17T13:26:35.091718321Z" level=info msg="storage: setting up Redis client"
May 17 13:26:35 aled chirpstack-application-server[4838]: time="2022-05-17T13:26:35.091925597Z" level=info msg="storage: connecting to PostgreSQL database"
May 17 13:26:35 aled chirpstack-application-server[4838]: time="2022-05-17T13:26:35.106444465Z" level=info msg="storage: applying PostgreSQL data migrations"
May 17 13:26:35 aled chirpstack-application-server[4838]: time="2022-05-17T13:26:35.191057804Z" level=fatal msg="get keys error: dial tcp: lookup redis on 127.0.0.53:53: server misbehaving"
May 17 13:26:35 aled systemd[1]: chirpstack-application-server.service: Main process exited, code=exited, status=1/FAILURE
May 17 13:26:35 aled systemd[1]: chirpstack-application-server.service: Failed with result 'exit-code'.
May 17 13:26:35 aled systemd[1]: chirpstack-application-server.service: Scheduled restart job, restart counter is at 4.
May 17 13:26:35 aled systemd[1]: Stopped ChirpStack Application Server.
May 17 13:26:35 aled systemd[1]: Started ChirpStack Application Server.
May 17 13:26:35 aled chirpstack-application-server[4844]: time="2022-05-17T13:26:35.60569202Z" level=info msg="starting ChirpStack Application Server" docs="https://www.chirpstack.io/" version=3.17.6
May 17 13:26:35 aled chirpstack-application-server[4844]: time="2022-05-17T13:26:35.606434564Z" level=info msg="storage: setting up storage package"
May 17 13:26:35 aled chirpstack-application-server[4844]: time="2022-05-17T13:26:35.606707794Z" level=info msg="storage: setup metrics"
May 17 13:26:35 aled chirpstack-application-server[4844]: time="2022-05-17T13:26:35.606959613Z" level=info msg="storage: setting up Redis client"
May 17 13:26:35 aled chirpstack-application-server[4844]: time="2022-05-17T13:26:35.607212474Z" level=info msg="storage: connecting to PostgreSQL database"
May 17 13:26:35 aled chirpstack-application-server[4844]: time="2022-05-17T13:26:35.622733573Z" level=info msg="storage: applying PostgreSQL data migrations"
May 17 13:26:35 aled chirpstack-application-server[4844]: time="2022-05-17T13:26:35.709754869Z" level=fatal msg="get keys error: dial tcp: lookup redis on 127.0.0.53:53: server misbehaving"
May 17 13:26:35 aled systemd[1]: chirpstack-application-server.service: Main process exited, code=exited, status=1/FAILURE
May 17 13:26:35 aled systemd[1]: chirpstack-application-server.service: Failed with result 'exit-code'.
May 17 13:26:36 aled systemd[1]: chirpstack-application-server.service: Scheduled restart job, restart counter is at 5.
May 17 13:26:36 aled systemd[1]: Stopped ChirpStack Application Server.
May 17 13:26:36 aled systemd[1]: Started ChirpStack Application Server.
May 17 13:26:36 aled chirpstack-application-server[4850]: time="2022-05-17T13:26:36.091303469Z" level=info msg="starting ChirpStack Application Server" docs="https://www.chirpstack.io/" version=3.17.6
May 17 13:26:36 aled chirpstack-application-server[4850]: time="2022-05-17T13:26:36.092084165Z" level=info msg="storage: setting up storage package"
May 17 13:26:36 aled chirpstack-application-server[4850]: time="2022-05-17T13:26:36.092329333Z" level=info msg="storage: setup metrics"
May 17 13:26:36 aled chirpstack-application-server[4850]: time="2022-05-17T13:26:36.092551047Z" level=info msg="storage: setting up Redis client"
May 17 13:26:36 aled chirpstack-application-server[4850]: time="2022-05-17T13:26:36.092815821Z" level=info msg="storage: connecting to PostgreSQL database"
May 17 13:26:36 aled chirpstack-application-server[4850]: time="2022-05-17T13:26:36.107475431Z" level=info msg="storage: applying PostgreSQL data migrations"
May 17 13:26:36 aled chirpstack-application-server[4850]: time="2022-05-17T13:26:36.194936001Z" level=fatal msg="get keys error: dial tcp: lookup redis on 127.0.0.53:53: server misbehaving"
May 17 13:26:36 aled systemd[1]: chirpstack-application-server.service: Main process exited, code=exited, status=1/FAILURE
May 17 13:26:36 aled systemd[1]: chirpstack-application-server.service: Failed with result 'exit-code'.
May 17 13:26:36 aled systemd[1]: chirpstack-application-server.service: Scheduled restart job, restart counter is at 6.
May 17 13:26:36 aled systemd[1]: Stopped ChirpStack Application Server.
May 17 13:26:36 aled systemd[1]: chirpstack-application-server.service: Start request repeated too quickly.
May 17 13:26:36 aled systemd[1]: chirpstack-application-server.service: Failed with result 'exit-code'.
May 17 13:26:36 aled systemd[1]: Failed to start ChirpStack Application Server.

Here is the conf of the application server:

[general]
log_level=4
[postgresql]
dsn="postgres://chirpstack_as:PASSWORD@localhost:5432/chirpstack_as?sslmode=disable"

[redis]
url="redis://redis:6379"

[application_server.integration]
marshaler="json_v3"
enabled=["mqtt"]
[application_server.integration.mqtt]
server="tcp://localhost:1883"
username="admin"
password="PASSWORD"

[application_server.api]
public_host="chirpstack-application-server:8001"

[application_server.external_api]
jwt_secret="verysecret"

I found my problem, it was that postgres wasn’t resolving “localhost”, I just replaced it with 127.0.0.1 and it was fixed.