|
| 1 | +--- |
| 2 | +title: How to enable auditing in PostgreSQL |
| 3 | +--- |
| 4 | + |
| 5 | +<HintBlock type="info"> |
| 6 | + |
| 7 | +PostgreSQL auditing allows you to track and log database activity, including user connections, query execution, and data modifications. Audit logs are crucial for security compliance, troubleshooting, and monitoring user activity in your database environment. |
| 8 | + |
| 9 | +Bytebase provides [centralized audit logging](/docs/security/audit-logging/) and [access control](/docs/security/data-access-control/) features that complement PostgreSQL's native auditing capabilities for enterprise environments. |
| 10 | + |
| 11 | +</HintBlock> |
| 12 | + |
| 13 | +## PostgreSQL's Built-in Logging |
| 14 | + |
| 15 | +PostgreSQL offers built-in logging capabilities that can be configured for basic auditing: |
| 16 | + |
| 17 | +### Configure Log Settings |
| 18 | + |
| 19 | +Add the following to your PostgreSQL configuration file (`postgresql.conf`): |
| 20 | + |
| 21 | +```plain |
| 22 | +# Basic logging settings |
| 23 | +log_destination = 'csvlog' |
| 24 | +logging_collector = on |
| 25 | +log_directory = 'log' |
| 26 | +log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' |
| 27 | +log_rotation_age = 1d |
| 28 | +log_rotation_size = 10MB |
| 29 | +
|
| 30 | +# What to log |
| 31 | +log_statement = 'all' # Options: none, ddl, mod, all |
| 32 | +log_connections = on |
| 33 | +log_disconnections = on |
| 34 | +log_duration = on |
| 35 | +``` |
| 36 | + |
| 37 | +- `log_statement`: Controls which SQL statements to log (none, ddl, mod, all) |
| 38 | +- `log_connections`: Log successful connections |
| 39 | +- `log_disconnections`: Log session terminations |
| 40 | +- `log_duration`: Include query execution time |
| 41 | + |
| 42 | +## pg_audit Extension (Recommended) |
| 43 | + |
| 44 | +For comprehensive auditing capabilities, install the `pgaudit` extension: |
| 45 | + |
| 46 | +### Install pgaudit |
| 47 | + |
| 48 | +For most distributions: |
| 49 | + |
| 50 | +```bash |
| 51 | +# Debian/Ubuntu |
| 52 | +sudo apt-get install postgresql-[version]-pgaudit |
| 53 | + |
| 54 | +# RHEL/CentOS |
| 55 | +sudo yum install pgaudit_[version] |
| 56 | +``` |
| 57 | + |
| 58 | +For source installation: |
| 59 | + |
| 60 | +```bash |
| 61 | +git clone https://github.com/pgaudit/pgaudit.git |
| 62 | +cd pgaudit |
| 63 | +make install |
| 64 | +``` |
| 65 | + |
| 66 | +### Configure pgaudit |
| 67 | + |
| 68 | +Add the following to your PostgreSQL configuration file (`postgresql.conf`): |
| 69 | + |
| 70 | +```plain |
| 71 | +# Load the extension |
| 72 | +shared_preload_libraries = 'pgaudit' |
| 73 | +
|
| 74 | +# Audit settings |
| 75 | +pgaudit.log = 'write, ddl' |
| 76 | +pgaudit.log_catalog = on |
| 77 | +pgaudit.log_parameter = on |
| 78 | +pgaudit.log_statement_once = on |
| 79 | +pgaudit.log_level = 'log' |
| 80 | +``` |
| 81 | + |
| 82 | +Then enable the extension in your database: |
| 83 | + |
| 84 | +```sql |
| 85 | +-- Connect to your database and run: |
| 86 | +CREATE EXTENSION pgaudit; |
| 87 | +``` |
| 88 | + |
| 89 | +### Audit Session Logging |
| 90 | + |
| 91 | +For session-level auditing, which audits operations by specific users: |
| 92 | + |
| 93 | +```sql |
| 94 | +-- Enable session audit logging for a user |
| 95 | +ALTER USER audited_user SET pgaudit.log = 'read, write'; |
| 96 | +``` |
| 97 | + |
| 98 | +### Object-level Audit Logging |
| 99 | + |
| 100 | +For more granular auditing of specific objects: |
| 101 | + |
| 102 | +```sql |
| 103 | +-- Create audit role |
| 104 | +CREATE ROLE auditor; |
| 105 | + |
| 106 | +-- Grant audit privileges on table |
| 107 | +GRANT SELECT ON sensitive_table TO auditor; |
| 108 | + |
| 109 | +-- Enable object-level auditing for the table |
| 110 | +ALTER TABLE sensitive_table ENABLE AUDIT; |
| 111 | +``` |
| 112 | + |
| 113 | +<HintBlock type="info"> |
| 114 | + |
| 115 | +For large databases, selective auditing using `pgaudit.log_relation` can help minimize performance impact by focusing only on important tables. |
| 116 | + |
| 117 | +Bytebase offers [SQL review](/docs/sql-review/overview/) and [data access control](/docs/security/data-access-control/) features that provide audit capabilities with less overhead. |
| 118 | + |
| 119 | +</HintBlock> |
| 120 | + |
| 121 | +## Alternative: WAL-based Auditing |
| 122 | + |
| 123 | +For advanced use cases, you can use write-ahead log (WAL) decoding for auditing: |
| 124 | + |
| 125 | +```plain |
| 126 | +# Enable logical decoding |
| 127 | +wal_level = logical |
| 128 | +max_replication_slots = 10 |
| 129 | +``` |
| 130 | + |
| 131 | +This approach is more advanced but allows real-time monitoring of all data changes. |
| 132 | + |
| 133 | +## Restart PostgreSQL |
| 134 | + |
| 135 | +After changing the PostgreSQL configuration, restart the service to apply the changes: |
| 136 | + |
| 137 | +```bash |
| 138 | +# For systemd-based systems |
| 139 | +sudo systemctl restart postgresql |
| 140 | + |
| 141 | +# For older systems |
| 142 | +sudo service postgresql restart |
| 143 | +``` |
| 144 | + |
| 145 | +## Verify Auditing is Enabled |
| 146 | + |
| 147 | +### For Built-in Logging |
| 148 | + |
| 149 | +```sql |
| 150 | +-- Check current logging settings |
| 151 | +SHOW log_statement; |
| 152 | +SHOW log_connections; |
| 153 | +SHOW logging_collector; |
| 154 | + |
| 155 | +-- View recent logs (if using csvlog format) |
| 156 | +SELECT * FROM pg_logical_slot_peek_changes('audit_slot', NULL, NULL); |
| 157 | +``` |
| 158 | + |
| 159 | +### For pgaudit Extension |
| 160 | + |
| 161 | +```sql |
| 162 | +-- Verify the extension is installed |
| 163 | +SELECT * FROM pg_extension WHERE extname = 'pgaudit'; |
| 164 | + |
| 165 | +-- Check current pgaudit settings |
| 166 | +SHOW pgaudit.log; |
| 167 | +SHOW pgaudit.log_catalog; |
| 168 | + |
| 169 | +-- Test with an auditable operation |
| 170 | +CREATE TABLE test_audit(id int); |
| 171 | +INSERT INTO test_audit VALUES (1); |
| 172 | +DROP TABLE test_audit; |
| 173 | +``` |
| 174 | + |
| 175 | +After these operations, check your log files for audit entries. |
| 176 | + |
| 177 | +<HintBlock type="info"> |
| 178 | + |
| 179 | +For enterprise environments managing multiple PostgreSQL instances, Bytebase provides [centralized schema change workflows](/docs/change-database/change-workflow/) with comprehensive [audit trails](/docs/security/audit-logging/) and [compliance checks](/docs/sql-review/review-policy/). This approach allows you to enforce consistent auditing policies across your database fleet. |
| 180 | + |
| 181 | +</HintBlock> |
| 182 | + |
| 183 | +## References |
| 184 | + |
| 185 | +- [PostgreSQL Logging Documentation](https://www.postgresql.org/docs/current/runtime-config-logging.html) |
| 186 | +- [pgAudit Extension](https://github.com/pgaudit/pgaudit) |
| 187 | +- [PostgreSQL WAL-based Auditing](https://www.postgresql.org/docs/current/logical-replication.html) |
| 188 | +- [Timescale Guide: What is Audit Logging in PostgreSQL](https://www.timescale.com/learn/what-is-audit-logging-and-how-to-enable-it-in-postgresql) |
0 commit comments