Refer to this, you should enable logging_collector
, then you can see incoming queries in log_directory
's log_filename
.
And to enable it in docker logs
, you had to make some trick to make it, one solution is as follows:
wrapper.sh:
#!/usr/bin/env bash
mkdir /logs
touch /logs/postgresql.log
chmod -R 777 /logs
tail -f /logs/* &
/docker-entrypoint.sh "$@"
Above will use tail
to monitor /logs/postgresql.log
which will later be used by postgresql
's logging_collector
, and show it docker logs
.
Dockerfile:
FROM postgres:11.1-alpine
COPY wrapper.sh /
RUN chmod +x /wrapper.sh
ENTRYPOINT ["/wrapper.sh"]
CMD ["postgres", "-c", "logging_collector=on", "-c", "log_directory=/logs", "-c", "log_filename=postgresql.log", "-c", "log_statement=all"]
Above will use customize wrapper.sh
, it will first monitor the postgre log, print it, and then contiune to execute the default docker-entrypoint.sh
to start postgresql server.
After container start, show logs before incoming queries:
orange@orange:~/abc$ docker build -t abc:1 .
orange@orange:~/abc$ docker run -idt abc:1
orange@orange:~/abc$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9112eb785e5 abc:1 "/wrapper.sh postgre…" 2 seconds ago Up 1 second 5432/tcp loving_joliot
orange@orange:~/abc$ docker logs loving_joliot
The files belonging to this database system will be owned by user "postgres".
......
2019-07-13 03:38:14.030 UTC [46] LOG: database system was shut down at 2019-07-13 03:38:13 UTC
2019-07-13 03:38:14.034 UTC [10] LOG: database system is ready to accept connections
Simulate some incoming queries, and see logs again:
orange@orange:~/abc$ docker exec -it -u postgres loving_joliot psql -c "SELECT datname FROM pg_database;"
datname
-----------
postgres
template1
template0
(3 rows)
orange@orange:~/abc$ docker logs loving_joliot
The files belonging to this database system will be owned by user "postgres".
......
2019-07-13 03:38:14.030 UTC [46] LOG: database system was shut down at 2019-07-13 03:38:13 UTC
2019-07-13 03:38:14.034 UTC [10] LOG: database system is ready to accept connections
2019-07-13 03:41:22.859 UTC [62] LOG: statement: SELECT datname FROM pg_database;
You can see above we simulate a sql execute SELECT datname FROM pg_database;
, and in docker logs
we could already see this sql.