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?
Solution 1:
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 thedata
directory, and that the postgres user can write to it.
Solution 2:
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
#log_directory = 'pg_log'
tolog_directory = 'pg_log'
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
tolog_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
#log_statement = 'none'
tolog_statement = 'all'
#logging_collector = off
tologging_collector = on
Optional:
SELECT set_config('log_statement', 'all', true);
sudo /etc/init.d/postgresql restart
orsudo service postgresql restart
Fire query in postgresql
select 2+2
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
Solution 3:
FYI: The other solutions will only log statements from the default database—usually postgres
—to log others; start with their solution; then:
ALTER DATABASE your_database_name
SET log_statement = 'all';
Ref: https://serverfault.com/a/376888 / log_statement
Solution 4:
SELECT set_config('log_statement', 'all', true);
With a corresponding user right may use the query above after connect. This will affect logging until session ends.