Errors while starting a docker compose project with postgreSQL

I have been facing difficulties in bringing a docker compose project for a NC instance. First, here’s my docker-compose.yaml:

version: '3.8'

services:
  adminer:
    image: adminer:4.8.1-standalone
    restart: always
    volumes:
      - adminer:/var/www/html
    environment:
      - ADMINER_DESIGN=dracula
    networks:
      - nextcloud-storage
    ports:
      - 8080:8080

  db:
    image: postgres:14.1-alpine
    restart: unless-stopped
    volumes:
      - db:/var/lib/postgresql/data
    env_file:
      - ./db.env
      - ./db.secret.env
    networks:
      - nextcloud-storage

  redis:
    image: redis:6.2.6-alpine
    command: redis-server --save 60 1 --loglevel warning
    restart: unless-stopped
    volumes:
      - redis:/data
    networks:
      - nextcloud-storage

  app:
    image: nextcloud:23.0.0-fpm-alpine
    restart: unless-stopped
    volumes:
      - nextcloud:/var/www/html
    env_file:
      - ./nextcloud.env
      - ./nextcloud.secret.env
      - ./db.env
      - ./db.secret.env
    depends_on:
      - db
      - redis
    networks:
      - nextcloud-storage
      - nextcloud-gateway

  # The entrypoint executes the command to initialise the cron from cron.php which performs some housekeeping tasks. 
  # See https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/background_jobs_configuration.html
  cron:
    image: nextcloud:23.0.0-fpm-alpine
    restart: unless-stopped
    volumes:
      - nextcloud:/var/www/html
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis
      
  web:
    image: caddy:2.4.6-alpine
    restart: unless-stopped
    volumes:
      - nextcloud:/var/www/html:ro
      - caddy_data:/data
      - ./Caddyfile:/etc/caddy/Caddyfile
    depends_on:
      - app
    networks:
      - nextcloud-gateway
      - ngrok-gateway

volumes:
  adminer:
    name: adminer
    driver_opts:
      o: bind
      device: ${ADMINER_VOLUME}
      type: none
  db:
    name: db
    driver_opts:
      o: bind
      device: ${DB_VOLUME}
      type: none
  redis:
    name: redis
    driver_opts:
      o: bind
      device: ${REDIS_VOLUME}
      type: none
  nextcloud:
    name: nextcloud
    driver_opts:
      o: bind
      device: ${APP_VOLUME}
      type: none
  caddy_data:
    name: caddy-nextcloud-data
    driver_opts:
      o: bind
      device: ${CADDY_VOLUME}
      type: none

networks:
  ngrok-gateway:
    name: ngrok-gateway
    external: true
  nextcloud-storage:
    name: nextcloud-storage
  nextcloud-gateway:
    name: nextcloud-gateway

The DB initialises ok (I think) but the app doesn’t initialise.
it starts with the following log outputs:

Configuring Redis as session handler

Initializing nextcloud 23.0.0.10 ...

Initializing finished

New nextcloud instance

Installing with PostgreSQL database

starting nextcloud installation

An exception occurred while executing a query: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "oc_users" does not exist

LINE 1: SELECT "uid", "displayname" FROM "oc_users" WHERE "uid_lower...

                                         ^

retrying install...

Error while trying to initialise the database: An exception occurred while executing a query: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "oc_appconfig" does not exist

LINE 1: SELECT * FROM "oc_appconfig"

                      ^

Trace: #0 /var/www/html/3rdparty/doctrine/dbal/src/Connection.php(1780): Doctrine\DBAL\Driver\API\PostgreSQL\ExceptionConverter->convert(Object(Doctrine\DBAL\Driver\PDO\Exception), Object(Doctrine\DBAL\Query))

At the same time I see loads of users getting created in the DB and they will be of the form oc_sayak, oc_sayak1, oc_sayak2 and so on. sayak is the value of the env var NEXTCLOUD_ADMIN_USER. I have tried with setting the POSTGRES_DB env in the db service and also tried not setting it. I have tried using postgres, nextcloud and sayak as POSTGRES_USER. But I get the same error each time. The environment variables are available inside the container so that shouldn’t be an issue. I am at my wits end. Any help will be appreciated.

Here is a sample env file:
db.env

POSTGRES_USER=nextcloud
POSTGRES_DB=nextcloud

db.secret.env

POSTGRES_PASSWORD=something

nextcloud.env

POSTGRES_HOST=db
REDIS_HOST=redis
NEXTCLOUD_ADMIN_USER=sayak

nextcloud.secret.env

NEXTCLOUD_ADMIN_PASSWORD=something

Note: The env vars in the compose volume section are provided using an .env file.