Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log PostgreSQL queries?

How to enable logging of all SQL executed by PostgreSQL 8.3?

Edited (more info) I changed these lines :

log_directory = 'pg_log'                     log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' log_statement = 'all' 

And restart PostgreSQL service... but no log was created... I'm using Windows Server 2003.

Any ideas?

like image 991
Paul Avatar asked Apr 06 '09 16:04

Paul


2 Answers

In your data/postgresql.conf file, change the log_statement setting to 'all'.


Edit

Looking at your new information, I'd say there may be a few other settings to verify:

  • make sure you have turned on the log_destination variable
  • make sure you turn on the logging_collector
  • also make sure that the log_directory directory already exists inside of the data directory, and that the postgres user can write to it.
like image 192
Jarret Hardie Avatar answered Oct 11 '22 06:10

Jarret Hardie


Edit your /etc/postgresql/9.3/main/postgresql.conf, and change the lines as follows.

Note: If you didn't find the postgresql.conf file, then just type $locate postgresql.conf in a terminal

  1. #log_directory = 'pg_log' to log_directory = 'pg_log'

  2. #log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' to log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

  3. #log_statement = 'none' to log_statement = 'all'

  4. #logging_collector = off to logging_collector = on

  5. Optional: SELECT set_config('log_statement', 'all', true);

  6. sudo /etc/init.d/postgresql restart or sudo service postgresql restart

  7. Fire query in postgresql select 2+2

  8. Find current log in /var/lib/pgsql/9.2/data/pg_log/

The log files tend to grow a lot over a time, and might kill your machine. For your safety, write a bash script that'll delete logs and restart postgresql server.

Thanks @paul , @Jarret Hardie , @Zoltán , @Rix Beck , @Latif Premani

like image 45
vijay Avatar answered Oct 11 '22 04:10

vijay