PostgreSQL pg_current_logfile: How to Use and Configure
In this article
The pg_current_logfile() function returns the name of the currently active log file for the current PostgreSQL session. This is primarily used by database administrators to quickly identify which physical file on the server disk is receiving the current stream of logs, which is essential for troubleshooting real-time errors or monitoring system performance without manually searching through the entire log directory.
What You'll Need
- A PostgreSQL instance with logging enabled.
- A database client (such as psql, pgAdmin, or DBeaver) connected to the server.
- Superuser privileges or the
pg_monitorrole (depending on the version) to execute the function. - The
logging_collectorparameter set toonin thepostgresql.conffile.
Step-by-Step
- Connect to your database: Open your preferred SQL interface and connect to the PostgreSQL server where you need to find the log file.
- Verify logging is active: Before running the function, ensure that the logging collector is enabled. You can check this by running:
If the result isSHOW logging_collector;off, thepg_current_logfile()function will returnNULLbecause the server is not managing log files through the collector. - Execute the function: Run the following SQL command to retrieve the current log filename:
SELECT pg_current_logfile(); - Interpret the result: The function will return a text string representing the filename (e.g.,
postgresql-2026-12-05_000000.log). Note that this is the filename only, not the full absolute path to the file on the server's file system. - Locate the full path: To find where this file is actually stored on the disk, run the following command to find the log directory:
Combine the result ofSHOW log_directory;log_directorywith the result ofpg_current_logfile()to get the complete path to the active log.
Common Mistakes
- Expecting a full file path: A common error is assuming
pg_current_logfile()provides the absolute path (e.g.,/var/lib/pgsql/data/log/postgresql.log). It only provides the name of the file. You must check thelog_directorysetting separately. - Running it with logging disabled: If
logging_collectoris set tooff, the function returnsNULL. This does not mean the server isn't logging; it may be logging tostderrorsyslog, which are handled by the operating system rather than the PostgreSQL collector. - Insufficient Permissions: Attempting to run this function as a low-privileged user may result in a permission error. Ensure the user has the necessary administrative roles.
Tips
- Combine for Efficiency: You can retrieve both the directory and the filename in a single query to get the full path immediately:
SELECT current_setting('log_directory') || '/' || pg_current_logfile(); - Use for Automation: This function is highly useful in monitoring scripts. Instead of using
ls -ton the server to find the newest file, the script can query the database directly to identify the correct log to tail. - Check Rotation: If you notice the filename changes frequently, check your
log_rotation_ageandlog_rotation_sizesettings inpostgresql.conf.
Troubleshooting
If the function is not working as expected, follow these steps in order:
- Check for NULL results: If the function returns
NULL, verify thatlogging_collector = onis set in your configuration. If you change this setting, you must restart the PostgreSQL service for it to take effect. - Verify Permissions: If you receive an "access denied" or "permission denied" error, grant the
pg_monitorrole to your user:GRANT pg_monitor TO your_username; - Confirm Log Destination: Run
SHOW log_destination;. If it is set tosyslogoreventlog,pg_current_logfile()will not work because the logs are being sent to an external system manager rather than a local file managed by PostgreSQL. - Check Disk Space: If logs are not appearing in the file returned by the function, check the server's disk space. If the disk is full, the collector may stop writing.
Frequently Asked Questions
Does pg_current_logfile work on all operating systems?
Yes, it works on Linux, Windows, and macOS, provided that the logging_collector is enabled and the logs are being written to files.
Can I use this function to read the contents of the log?
No. This function only tells you the name of the file. To read the logs, you must have access to the server's file system or use a tool like pg_read_file() (which requires superuser privileges).
Why is my log filename different every day?
This is due to the log_filename configuration setting. By default, PostgreSQL often includes the date in the filename to organize logs by day.
What happens to the result after a log rotation?
As soon as the server rotates the log to a new file, pg_current_logfile() will immediately begin returning the name of the new active file.
Is there a performance hit to calling this function?
No. The function retrieves a value already known to the server process; it does not perform a heavy disk scan or complex calculation.