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?
logging_collector = on
systemctl restart postgresql
may not actually restart PostgreSQL service you have configured (I don't understand why yet), so changes in the configuration file won't be applied. It is safer to use pg_ctl
(or pg_ctlcluster
on Debian).
systemctl reload postgresql
, systemctl restart postgresql
, service postgresql reload
and service postgresql restart
all render configuration changes effective.
ALTER DATABASE
(as in this answer)
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.
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' to log_directory = 'pg_log' #log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' to log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' #log_statement = 'none' to log_statement = 'all' #logging_collector = off to logging_collector = on Optional: SELECT set_config('log_statement', 'all', true); sudo /etc/init.d/postgresql restart or sudo 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
# log_destination = 'stderr'
in the configuration file before this worked.
log_filename = 'postgresql-%d.log'
and no it won't overwrite after each restart, it will append for each day and overwrite each month. Of course there are different days depending on month 28,29,30,31 -- but you get the idea.
select pg_reload_config();
since Postgres 9.0 rather than restarting the service in step 6.
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
log_statement = all
in postgresql.conf
for the respective database cluster - like the accepted answer suggests - affects all databases of that cluster - which can be overruled by per-database settings.
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.
SET log_statement = 'all'
or (for transaction level) SET LOCAL log_statement = 'all'
. You might also be interested in the client_min_messages
and log_min_messages
settings.
permission denied to set parameter "log_statement"
since my user is not superuser.
GRANT { EXECUTE | ALL [ PRIVILEGES ] } ON { FUNCTION function_name ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) [, ...] | ALL FUNCTIONS IN SCHEMA schema_name [, ...] } TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
You also need add these lines in PostgreSQL and restart the server:
log_directory = 'pg_log'
log_filename = 'postgresql-dateformat.log'
log_statement = 'all'
logging_collector = on
Required to be on for csvlogs
, thinking this option was to log query output and not just statements, but it's not the case.
+1 to above answers. I use following config
log_line_prefix = '%t %c %u ' # time sessionid user
log_statement = 'all'
Just to have more details for CentOS 6.4 (Red Hat 4.4.7-3) running PostgreSQL 9.2, based on the instructions found on this web page:
Set (uncomment) log_statement = 'all' and log_min_error_statement = error in /var/lib/pgsql/9.2/data/postgresql.conf. Reload the PostgreSQL configuration. For me, this was done by running /usr/pgsql-9.2/bin/pg_ctl reload -D /var/lib/pgsql/9.2/data/. Find today's log in /var/lib/pgsql/9.2/data/pg_log/
pg_ctl reload
is sufficient, and does not interrupt connections. Not convinced this answer adds anything to those already here.
There is an extension in postgresql for this. It's name is "pg_stat_statements". https://www.postgresql.org/docs/9.4/pgstatstatements.html
Basically you have to change postgresql.conf file a little bit:
shared_preload_libraries= 'pg_stat_statements'
pg_stat_statements.track = 'all'
Then you have to log in DB and run this command:
create extension pg_stat_statements;
It will create new view with name "pg_stat_statements". In this view you can see all the executed queries.
You should also set this parameter to log every statement:
log_min_duration_statement = 0
I was trying to set the log_statement
in some postgres config file but in fact the file was not read by our postgres.
I confirmed that using the request :
select *
from pg_settings
[...]
log_statement none # That was not the value i was expected for !!!
I use this way https://stackoverflow.com/a/41912295/2294168
command: postgres -c config_file=/etc/postgresql.conf
Dynamically we can enable/disable the logging in 2 ways
Change the global variables in DB and reload the configuration a) Set log_statement = 'all'; or set log_min_duration_statement = 0; b) select pg_reload_conf(); From the Linux command line, edit the postgres configuration file, change the log related parameters log_min_duration_statement = 0 log_statement = 'all' Reload the configuration file su - postgres /usr/bin/pg_ctl reload
In both these cases, we should not be doing a Postgres restart. We can dynamically enable/disable logging with configuration reload.
I hope this should be helpful.
Success story sharing
pg_ctl reload
ALTER SYSTEM
command a superuser can set GUC params from SQL.data
directory cited in the answer is not its literal name; it refers to the path assigned to thedata_directory
variable in the PostgreSQL configuration file. On Debian and Ubuntu GNU/Linux, this file usually resides at/etc/postgresql/$v/main/postgresql.conf
, where$v
is the server version. Also, on the aforementioned systems, whenlog_destination = 'stderr'
, the output is written to/var/log/postgresql/postgresql-$v-main.log
, where$v
is the server version (not to some location insidedata_directory
).