Skip to content

Commit bd3eb0e

Browse files
committed
chore: refine postgres audit logging
1 parent 513bf01 commit bd3eb0e

File tree

2 files changed

+12
-37
lines changed

2 files changed

+12
-37
lines changed
Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: Postgres Audit Log Guide
2+
title: Postgres Audit Loggging Guide
33
author: Adela
44
updated_at: 2025/11/10 18:00:00
5-
feature_image: /content/blog/postgres-audit-log/cover.webp
5+
feature_image: /content/blog/postgres-audit-logging/cover.webp
66
tags: Explanation
77
description: A guide to audit logging in Postgres.
88
---
@@ -13,9 +13,7 @@ In this guide, we’ll walk through the most practical approaches — from Postg
1313

1414
## 1. Native PostgreSQL Logging
1515

16-
[PostgreSQL logging documentation](https://www.postgresql.org/docs/current/runtime-config-logging.html)
17-
18-
PostgreSQL comes with a robust logging subsystem out of the box. It’s often the first step in building an audit trail.
16+
PostgreSQL comes with a [logging subsystem](https://www.postgresql.org/docs/current/runtime-config-logging.html) out of the box. It’s often the first step in building an audit trail.
1917

2018
**Key settings**
2119

@@ -44,12 +42,8 @@ This setup tells PostgreSQL to collect all executed SQL statements, include who
4442

4543
You can use tools like [pgBadger](https://github.com/darold/pgbadger) to analyze these logs and generate visual reports of who executed what queries and when.
4644

47-
➡️ *Takeaway:* Native logging is the minimum audit layer every PostgreSQL instance should have enabled.
48-
4945
## 2. Trigger-Based Auditing
5046

51-
[PostgreSQL Triggered Change Notification (tcn) documentation](https://www.postgresql.org/docs/current/tcn.html)
52-
5347
If you need to record **row-level changes** — for example, before and after values on UPDATE — you can use triggers.
5448

5549
**Example**
@@ -96,15 +90,11 @@ This captures all DML operations (`INSERT`, `UPDATE`, `DELETE`) on the `users` t
9690
- Must be defined per table.
9791
- Can impact performance on write-heavy workloads.
9892

99-
For real-time change notifications, PostgreSQL offers the `tcn` module (Triggered Change Notification), which can be used to send `NOTIFY` events to listening clients when data changes.
100-
101-
➡️ *Takeaway:* Trigger-based auditing gives detailed change tracking for sensitive tables — best used selectively.
93+
For real-time change notifications, PostgreSQL offers the [tcn](https://www.postgresql.org/docs/current/tcn.html) module (Triggered Change Notification), which can be used to send `NOTIFY` events to listening clients when data changes.
10294

10395
## 3. pgAudit Extension
10496

105-
[Visit the pgAudit GitHub repository](https://github.com/pgaudit/pgaudit)
106-
107-
For structured, compliance-grade audit logs, PostgreSQL’s **pgAudit** extension is the standard choice.
97+
For structured, compliance-grade audit logs, PostgreSQL’s [pgAudit](https://github.com/pgaudit/pgaudit) extension is the standard choice.
10898
It extends native logging to provide more context and granularity, especially around read/write operations.
10999

110100
**Installation**
@@ -125,7 +115,7 @@ pgaudit.log_catalog = off
125115

126116
After restarting PostgreSQL, you’ll start seeing audit logs like:
127117

128-
```
118+
```plain
129119
AUDIT: SESSION,1,READ,SELECT,,,,"SELECT * FROM customers WHERE id=42;",<none>
130120
```
131121

@@ -140,20 +130,15 @@ AUDIT: SESSION,1,READ,SELECT,,,,"SELECT * FROM customers WHERE id=42;",<none>
140130
- Logs can be verbose — use `pgaudit.log_parameter = off` to reduce noise.
141131
- Requires proper log rotation and analysis strategy.
142132

143-
➡️ *Takeaway:* pgAudit is the go-to choice for organizations that need **detailed, compliant** audit trails.
144-
145133
## 4. Bytebase
146134

147-
[See the Bytebase audit log documentation](https://docs.bytebase.com/security/audit-log)
148-
149-
Bytebase is a Database DevSecOps platform that provides a **centralized audit trail protected from unauthorized modification** across your PostgreSQL environments.
150-
It records *who did what, when, and why* — linking SQL actions to their **context** (issues, approvals, and deployments) while keeping sensitive data secure.
135+
Bytebase is a Database DevSecOps platform that provides a [centralized audit trail](https://docs.bytebase.com/security/audit-lo) across your PostgreSQL environments.
136+
It records _who did what, when, and why_ — linking SQL actions to their context (issues, approvals, and deployments) while keeping sensitive data secure.
151137

152138
**What Bytebase Audits**
153139

154-
- **Query access:** logs *who queried which data* and *when* across SQL Editor, Admin Query, and Data Export.
155-
Thanks to **dynamic data masking**, Bytebase only stores the **executed SQL statements** and metadata — **never the actual query results**.
156-
- **Schema and data changes:** tracks *who made which changes*, *when they were approved*, and *through which workflow or Git commit*.
140+
- **Query access:** logs _who queried which data_ and _when_ across SQL Editor, Admin Query, and Data Export.
141+
- **Schema and data changes:** tracks _who made which changes_, _when they were approved_, and _through which workflow or Git commit_.
157142
- **Governance controls:** built-in SQL review rules, approval flow, and role-based access help prevent unauthorized actions.
158143

159144
**Why It Matters**
@@ -162,17 +147,7 @@ It records *who did what, when, and why* — linking SQL actions to their **cont
162147
- **Privacy-safe auditing** with no sensitive data exposure.
163148
- **Compliance-ready** logs aligned with SOC 2, ISO 27001, and GDPR.
164149

165-
➡️ *Takeaway:* Unlike pgAudit, which records statements at the database level, Bytebase captures **who accessed or changed data, under which approval, without exposing sensitive information** — a privacy-first audit trail for modern teams.
166-
167-
## Best Practices
168-
169-
Audit logging can generate large volumes of data, so design it carefully.
170-
171-
- **Centralize and retain logs:** forward to ELK, Datadog, or S3.
172-
- **Avoid sensitive data:** mask or omit personal identifiers.
173-
- **Rotate regularly:** control log size and prevent disk exhaustion.
174-
- **Test impact:** measure overhead before enabling full-statement logging.
175-
- **Layer approaches:** combine pgAudit (low-level) with Bytebase (change workflow) for complete visibility.
150+
Unlike other solutions, which records statements at the database level, Bytebase captures **who accessed or changed data, under which approval, without exposing sensitive information**. You can also call the [API](https://docs.bytebase.com/integrations/api/audit-log) to send the audit logs to a centralized log sink.
176151

177152
## Conclusion
178153

@@ -186,4 +161,4 @@ PostgreSQL offers multiple layers of auditing — from basic text logs to comple
186161

187162
- Optionally, use **triggers** for fine-grained row-level auditing on critical tables.
188163

189-
By combining these layers, you gain both the visibility and control needed for secure, compliant, and well-governed database operations.
164+
By combining these layers, you gain both the visibility and control needed for secure, compliant, and well-governed database operations.
File renamed without changes.

0 commit comments

Comments
 (0)