Updated May 17, 2023
Definition of PostgreSQL Log Queries
We can enable logging in PostgreSQL by modifying the configuration file provided by PostgreSQL. PostgreSQL provides the configuration file named ‘postgresql. conf’, which is used to configure the various settings. In order to have the effect applied, it is necessary to restart the PostgreSQL service whenever we perform some modification in the configuration file. The PostgreSQL allows us to enable the temporary logging for a particular client’s session and modify the configuration file settings in memory only. The PostgreSQL configuration option named logging_collector option needed to be ON to use the csvlog as log_destination. In this article, we will learn in detail about PostgreSQL Log Queries.
Syntax:
- log_destination: This defines the different ways of logging messages, such as stderr, csvlog, eventlog, and syslog, etc.
- logging_collector: It is enabled for capturing the stderr and csvlog.
- log_directory: This defines the directory or folder in which the log files are getting generated.
- log_filename: ‘PostgreSQL-%Y-%m-%d_%H%M%S.log’ This defines the name of the log file with a timestamp. For e.g PostgreSQL-2020-06-29_111948.log
How does Log Queries work in PostgreSQL?
In order to log the queries in PostgreSQL, we need to modify the PostgreSQL configuration file named ‘postgresql. conf’. If you are unaware of the PostgreSQL configuration file’s location, you can easily find it by using the SHOW command.
We can locate the PostgreSQL configuration file by using the following command.
SHOW config_file;
Illustrate the result of the above statement by using the following statement:
Similarly, we can locate the data directory by using the following command.
SHOW data_directory;
Illustrate the result of the above statement by using the following statement:
Alter the PostgreSQL configuration file named as ‘postgresql. conf’ for logging queries.
The options like log_directory, log_filename, log_file_mode, log_truncate_on_rotation, log_rotation_age, and log_rotation_size can be used only if the PostgreSQL configuration option logging_collector is on.
We need to perform the following steps in order to enable PostgreSQL logging.
- Open the ‘postgresql. conf’ in any text editor.
- Scroll down to the section named ‘ERROR REPORTING AND LOGGING’. In this section, we can see many of the configuration options are commented.
- In order to enable the queries logging, we need the log_destination and logging_collector options.
Now, have a look at the snippet of the configuration file.
- We need to uncomment the log_directory and log_filename configuration options. The log_filename option includes the timestamp in the name of the log files.
- In it is necessary to have the logging_collector set to ON for performing the queries logging.
- Now restart the PostgreSQL service.
It is mandatory to restart the PostgreSQL service for having the modified configuration settings in effect. On windows, we can go to the services, perform the right-click, and then click on the restart menu.
Have a look at the code snippet in order to restart the PostgreSQL service.
Illustrate the PostgreSQL service restart by using the following snapshot.
- Verify the PostgreSQL query logging.
After performing step 6, the PostgreSQL immediately starts the logging. We know the path to the data directory as we have seen how to locate it.
For verifying the PostgreSQL log, go to the installation folder of PostgreSQL and navigate to ‘\ data\log’ folder and list the log files,
Illustrate the log files by using the following snapshot.:
Examples
In the above section, we have seen how to enable logging. Now we will execute some queries then we will see the generated log file contents.
Consider the following statement, which throws an exception where we are getting an error as datatype vqrchar2 does not exist.
create table testtb(id int, name varchar2(10));
We can set the log_destination as ‘csvlog’ or ‘stderr’ as per our requirements.
The PostgreSQL configuration option named logging_collector option needed to be ON in order to use the csvlog as log_destination.
Illustrate the content of the .csv log file, as we have specified the log_destination as csvlog.
Illustrate the content of the .csv log file, as we have specified the log_destination as stderr.
Conclusion
We hope from the above article, you have understood how the PostgreSQL Log queries work with doing some modifications in the configuration file. Also, how the log files are automatic gets generated by modifying some options in the PostgreSQL configuration file. We have added several examples of the PostgreSQL Log queries to understand them in detail.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL Log Queries” was beneficial to you. You can view EDUCBA’s recommended articles for more information.