How to Log PostgreSQL Query Statements

Last updated: April 1, 2025

On Aptible, you can set up Log Drains to deliver logs to various logging destinations like Papertrail, Sumologic, and Datadog. This includes database logs, but by default, most databases do not log every query that's run against it.

If you would like to log all queries, PostgreSQL makes it simple through the log_statement configuration variable. By default, this is set to none to prevent an oversaturation of logs, but can also be set to ddl, mod, or all.

  • none - no queries are logged

  • ddl - data definition statements such as CREATE, DROP, and ALTER are logged

  • mod - On top of ddl, additional data-modifying statements like INSERT , UPDATE, DELETE , and TRUNCATE are logged.

  • all - all queries are logged

I would take a look at the PostgreSQL documentation linked above for a more detailed explanation of each value, and also any related configuration variables you may want to set.

Once you've decided what to set it to, you can update your PostgreSQL database's logging behavior through the following statements; no restart necessary:

ALTER SYSTEM SET log_statement = 'all';
SELECT pg_reload_conf();