Wednesday, July 30, 2025

Step-by-Step Runbook: Enabling Logging in PostgreSQL 13.21

 Step-by-Step Runbook: Enabling Logging in PostgreSQL 13.21


### 1. Identify Configuration File and Data Directory


- Connect to your PostgreSQL database using `psql`.

- Run:

  ```

  SHOW config_file;

  SHOW data_directory;

  ```

- Note down the returned paths for use in the next steps.


### 2. Edit the `postgresql.conf` File


- Open the `postgresql.conf` file in a text editor (replace the path as appropriate):


  ```sh

  sudo vi /var/lib/pgsql/13/data/postgresql.conf

  ```


- In `postgresql.conf`, find and set these parameters:


  ```

  logging_collector = on

  log_destination = 'csvlog'           # Or 'stderr', or use both separated by comma

  log_directory = 'pg_log'             # Folder for logs; default is relative to data_directory

  log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

  log_statement = 'ddl'                # 'all' for everything, 'ddl' or 'mod' for less

  log_connections = on

  log_disconnections = on

  ```


  > *Uncomment the lines by removing any leading `#`.*


### 3. Save and Exit


- Write your changes and exit the editor.


### 4. Restart PostgreSQL Service


- Restart the PostgreSQL service to apply logging changes:


  ```sh

  sudo systemctl restart postgresql-13

  ```


  - Or, based on your system:

  

    ```sh

    sudo service postgresql-13 restart

    ```


### 5. Verify Logging


- After restart, check the log directory, for example:


  ```sh

  cd /var/lib/pgsql/13/data/pg_log

  ls -lh

  ```


- Open a recent log file to confirm logs are being generated.


### 6. (Optional) Use ALTER SYSTEM for Dynamic Logging (Some Parameters Only)


- For some log settings (not `logging_collector`), you can use:


  ```sql

  ALTER SYSTEM SET log_statement = 'ddl';

  SELECT pg_reload_conf();

  ```


  > *A full restart is required for collector activation; parameter reload is sufficient for most log detail settings.*


You have now enabled and verified logging for your PostgreSQL 13.21 installation, ensuring SQL activities and session events are recorded for review and auditing.

No comments:

Post a Comment