PostgreSQL logs can become very large and may need some routine maintenance to prevent them from filling up C:\ drive capacity. Logs are invaluable when diagnosing problems. However, the PostgreSQL log output can become extremely large (5 GB x day), especially at higher debug levels.
To handle the deletion of the older unnecessary log files, we create a schedule job to run regularly to delete the logs older than 15 days.
GSX Gizmo 1.3+ | PostgreSQL
Instructions
- Open Notepad
- Paste the following PowerShell command:
-
$start = (get-date).AddDays(-15)
Get-ChildItem -Path C:\ProgramData\PostgreSQL\10\Data\log\*.log | where {$PSItem.LastWriteTime -lt $start} | Remove-Item
-
- Save as a LogClean.ps1 file for example:
- Open the Windows Task Scheduler
- You can locate the Windows Task Scheduler by searching for it in the Windows Start Menu.
- Select Create Basic Task:
- Name the task, such as Clean Up Log Files and click Next:
- Select a timeframe for a Daily Trigger when the server is not busy, such as 4AM:
- Click Next and select Start a program:
- Click Browse... and select the .ps1 file created in Step 1 and click Next:
- Select the Open the Properties dialog box for this task when I click Finish option:
- Then click Run whether user is logged on or not and Run with highest privileges:
- Click OK - Finish.
You now have a script that is set to run every day to clear the PostgreSQL Logs. You can also adjust the frequency of how often the script runs if necessary.