How to log PostgreSQL queries?

Better Stack Team
Updated on November 23, 2022

If you would like to log all PostgreSQL queries into a file, you can do that by changing few settings int the configuration file

Open the data/postgresql.conf file and change the following settings:

  • #log_directory = 'pg_log’ change to log_directory = 'pg_log’
  • #log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log’ change to log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
  • #log_statement = 'none’ change to log_statement = 'all’
  • #logging_collector = off change to logging_collector = on
  • Run the following statement:
SELECT set_config('log_statement', 'all', true);
  • Then restart the PostgreSQL server
sudo service postgresql restart

Now, you can find the logs in the following directory /var/lib/pgsql/9.2/data/pg_log/

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →