'Enable logging in postgresql using docker-compose

I am using Postgres as a service in my docker-compose file. I want logging to log file to be enabled when I do docker-compose up. One way to enable logging is by editing postgres.conf file but it's not useful in this case. One other way is to do something like this

docker run --name postgresql -itd --restart always sameersbn/postgresql:10-2 -c logging_collector=on

but this isn't useful too cause I am not starting it from an image but as a docker-compose service. Any idea how I can start the docker-compose up with logging enabled in Postgres???



Solution 1:[1]

  • Just like you command with docker run:
docker run --name postgresql -itd --restart always sameersbn/postgresql:10-2 -c logging_collector=on

that you add the -c logging_collector=on arguments for the ENTRYPOINT ["/sbin/entrypoint.sh"] to enable logging. (Dockerfile).

  • In docker-compose.yml file, use command: like this:

version: "3.7"
services:
  database:
    image: sameersbn/postgresql:10-2
    command: "-c logging_collector=on"

# ......

When Postgresql contaienr run, it will run command: /sbin/entrypoint.sh -c logging_collector=on.

Solution 2:[2]

Here is the docker-compose to run the command -c in compose

          version: '3.6'
          services:
            postgresql:
              image: postgres:11.5
              container_name: platops_postgres
              volumes: ['platops-data:/var/lib/postgresql/data/', 'postgress-logs:/var/log/postgresql/']
              command: ["postgres", "-c", "logging_collector=on", "-c", "log_directory=/logs", "-c", "log_filename=postgresql.log", "-c", "log_statement=all"]
              environment:
                - POSTGRES_USER=postgresql
                - POSTGRES_PASSWORD=postgresql
              ports: ['5432:5432']
          volumes:
            platops-data: {}
              # uncomment and set the path of the folder to maintain persistancy
              # data-postgresql:
              # driver: local
              # driver_opts:
              #   o: bind
              #   type: none
              #   device: /path/of/db/postgres/data/
            postgress-logs: {}
              # uncomment and set the path of the folder to maintain persistancy
              # data-postgresql:
              # driver: local
              # driver_opts:
              #   o: bind
              #   type: none
              #   device: /path/of/db/postgres/logs/

For more information, you can check with the containers/postgress

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lamth
Solution 2