I get current log file by using SQL without getting in server via ssh. So I use a query like the one below;

SELECT pg_read_binary_file(

Log files are not too big so i am fine with that. The thing is, I cannot use this query in a generic way. Is there a way to get 'postgresql-Fri.log' from my log_filename setting which is currently postgresql-%a.log but the format can differ?

1 Answer 1


There is now a pg_current_logfile() function returning this information:

Primary log file name, or log in the requested format, currently in use by the logging collector.

but it's a new feature of PostgreSQL 10, which you probably don't run since it hasn't yet got out as a General Availability release (it's still Release Candidate at the moment).

With previous versions, you're pretty much on your own to figure out the current name, either by interpreting log_filename, or by finding the latest modified, for instance with a combination of pg_ls_dir and pg_stat_file:

SELECT file, -- should be the latest logfile
  FROM  pg_ls_dir(current_setting('log_directory')||'/') as list(file)
  LIMIT 1;

This can fail if the server's clock goes backward or if non-log files are dumped into the same directory as the log files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.