> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/server-administration/maintenance/logfile-maintenance.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 24.3. 日誌檔案維護 \#

### 24.3. 日誌檔案維護 [#](#logfile-maintenance) <a href="#logfile-maintenance" id="logfile-maintenance"></a>

將資料庫伺服器的日誌輸出保存下來，而不是單純透過 `/dev/null` 予以捨棄，是個好主意。 日誌輸出在診斷問題時具有無可取代的價值。

#### 注意

伺服器日誌可能包含敏感資訊，因此無論其儲存方式、儲存位置， 或是被導向的目的地為何，都需要加以保護。 舉例來說，部分 DDL 陳述式可能包含明文密碼或其他驗證細節。以 `ERROR` 層級記錄的陳述式，可能會顯示應用程式的 SQL 原始碼， 也可能包含部分資料列的內容。記錄資料、事件與 相關資訊本來就是這項功能所預期的用途，因此這並不是 資訊外洩或臭蟲。請確保伺服器日誌僅對經過適當授權的 人員可見。

日誌輸出往往非常龐大 （尤其是在較高的除錯層級時），因此你不會想要無限期地保存它。你需要\*輪替（rotate）\*日誌檔案，讓系統在經過一段合理 時間之後開始使用新的日誌檔案，並移除舊的日誌檔案。

如果你只是將 `postgres` 的 stderr 導向到某個檔案，你會得到日誌輸出，但 截斷該日誌檔案的唯一方式，就是停止並重新啟動 伺服器。如果你是在開發環境中使用 PostgreSQL，這樣的作法或許可以接受， 但很少有正式環境（production）的伺服器會認為這種行為是可以接受的。

比較好的做法是把伺服器的 stderr 輸出，傳送給某種日誌輪替程式。 系統內建了一套日誌輪替機制，你可以透過 在 `postgresql.conf` 中將組態參數 `logging_collector` 設為 `true` 來啟用它。這套機制的控制 參數說明於 [第 19.8.1 節](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-logging.md#runtime-config-logging-where)。你也可以透過這種方式 擷取以機器可讀的 CSV （comma-separated values，逗號分隔值）格式呈現的日誌資料。

或者，如果你已經在其他伺服器軟體上使用某個 外部日誌輪替程式，你可能會想繼續沿用它。 舉例來說，Apache 發行版所附的 rotatelogs 工具，就可以搭配 PostgreSQL 使用。其中一種 做法是把伺服器的 stderr 輸出透過管線（pipe）傳給所要使用的程式。 如果你是使用 `pg_ctl` 來啟動伺服器，stderr 就已經被重新導向到 stdout，所以你只需要一個 管線指令，例如：

```

pg_ctl start | rotatelogs /var/log/pgsql_log 86400
```

你可以結合這兩種做法，設定 logrotate 來收集由 PostgreSQL 內建日誌收集器所產生的日誌檔案。在這種情況下，日誌收集器負責定義日誌檔案的名稱與 位置，而 logrotate 會定期將這些檔案封存。在開始進行日誌輪替時， logrotate 必須確保應用程式會將 後續的輸出傳送到新的檔案。這通常是透過一支 `postrotate` 指令稿來完成，該指令稿會向應用程式送出 `SIGHUP` 訊號，讓應用程式重新開啟日誌檔案。 在 PostgreSQL 中，你則可以改用帶有 `logrotate` 選項的 `pg_ctl` 來執行此操作。當伺服器收到這個指令時，會依據日誌組態， 切換到新的日誌檔案，或是重新開啟 現有的檔案（參見 [第 19.8.1 節](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-logging.md#runtime-config-logging-where)）。

#### 注意

當使用靜態的日誌檔案名稱時，如果達到最大開啟檔案數量限制， 或發生檔案表溢位，伺服器可能無法重新開啟日誌檔案。 在這種情況下，日誌訊息會持續傳送到舊的日誌檔案， 直到下一次成功的日誌輪替為止。如果 logrotate 被 設定為壓縮並刪除日誌檔案，伺服器可能會遺失 在這段時間內所記錄的訊息。為避免這個問題，你可以將 日誌收集器設定為動態指派日誌檔案名稱， 並使用 `prerotate` 指令稿來忽略已開啟的日誌檔案。

另一種正式環境等級（production-grade）的日誌輸出管理方式， 是將輸出傳送給 syslog，交由 syslog 處理檔案輪替。若要這麼做，可 在 `postgresql.conf` 中將組態參數 `log_destination` 設為 `syslog` （表示只記錄到 syslog）。 之後，只要你想強制 syslog daemon 開始寫入新的日誌檔案，就可以送出 `SIGHUP` 訊號給它。如果你想要自動化日誌 輪替，logrotate 程式也可以被設定成能夠 搭配來自 syslog 的日誌檔案運作。

不過，在許多系統上，syslog 的可靠性並不高， 尤其是在處理大型日誌訊息時；它可能會恰好在你最需要 這些訊息的時候，將其截斷或遺失。此外，在 Linux 上，syslog 會將每一則訊息都刷寫 至磁碟，導致效能不佳。（你可以在 syslog 組態檔中，於檔案名稱開頭加上「`-`」， 以停用同步寫入。）

請注意，以上所描述的所有解決方案，都只負責以 可設定的間隔時間開始使用新的日誌檔案，但並不處理 刪除已不再需要的舊日誌檔案。你可能會想要設定一個 批次工作，定期刪除舊的日誌檔案。另一種 可行的做法，是將輪替程式設定為讓舊的日誌檔案以 循環方式被覆寫。

[pgBadger](https://pgbadger.darold.net/) 是一個外部專案，能對日誌檔案進行精密複雜的分析。 [check\_postgres](https://bucardo.org/check_postgres/) 能在日誌檔案中出現重要訊息時提供 Nagios 警示， 並能偵測許多其他異常狀況。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/logfile-maintenance.html)（原文版本：18.6；核對日期：2026-09-25）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/server-administration/maintenance/logfile-maintenance.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
