Or, “The Eye of Sauron Sees All… Once You apt install
It”
Welcome back, fellow travelers on the digital frontier! After our Epic Quest for Secure Passwords, you’d think we could rest. But in the world of data security, there is no rest. There is only the eternal vigilance of the All-Seeing Eye. đď¸
Today, we embark on a new quest: to implement pgAudit
in PostgreSQL. Why? Because simply having a strong fortress gate (authentication) isn’t enough. You need to know what’s happening inside the walls. Who accessed the royal treasury? Who tried to rewrite history (or, you know, the employees
table)?
PostgreSQL’s native log_statement
setting is… a thing. It’s like strapping a GoPro to a caffeinated squirrel and hoping you capture the one moment a crime was committed. Itâs noisy, unstructured, and will log everything, including things you absolutely do not want logged. pgAudit
, on the other hand, is a precision instrument. Itâs a dedicated, C-written extension that provides the detailed, structured, and granular audit logs you need to satisfy the Watchers on the Wall (or your friendly neighborhood compliance auditors).
This series will guide you through installing, configuring, and mastering pgAudit
, turning your database logs from a chaotic mess into a pristine record of every important action. Let’s begin by forging our new ring of power.
A Quick Word of Warning (The Disclaimer)
Disclaimer: This article is a technical showcase, not a substitute for professional consultancy. The configurations provided are for educational purposes. They haven’t been battle-tested in a production environment under the watchful eye of a dragon (or your friendly neighborhood compliance auditors). Use these configurations at your own risk. Don’t come crying to me if you accidentally summon a Balrog in your data center.
Installing pgAudit: You Have My Sword… and My apt-get
So, what’s the first step to get started with pgAudit
? Before we can use it, we have to install it. Like any good RPG side quest, this requires a few prerequisites. Make sure your server has the basic development tools ready to go.
Prerequisites:
make
gcc
- The correct PostgreSQL development headers for your version (e.g.,
postgresql-server-dev-16
on Debian/Ubuntu,postgresql16-devel
on RHEL/Fedora).
You have two paths before you. Choose wisely.
Path 1: The Path of the Hobbit (Using Your Package Manager)
This is the easy, straightforward path. Itâs less work, which means more time for second breakfast. For example, this guide will show you how to install pgAudit
on Ubuntu using apt
, but the process is similar for other systems. Use your distribution’s package manager to install the pgaudit
package that matches your PostgreSQL version.
For Debian/Ubuntu:
sudo apt update && sudo apt install postgresql-16-pgaudit
For RHEL/Fedora/CentOS:
sudo dnf install pgaudit16_16
(Note: The package name can vary depending on the repository, so you might have to search for it with dnf search pgaudit
).
Path 2: The Path of the Dwarf (Forging It from Source)
Sometimes the package you need isn’t available, or you need a very specific version. This is for the adventurers among us who prefer to forge their own tools from the raw source code. Itâs more work, but the bragging rights are immense.
First, clone the official pgaudit
repository from GitHub.
git clone https://github.com/pgaudit/pgaudit.git
Navigate into the new directory and compile the extension. The USE_PGXS=1
flag tells make
to use the PostgreSQL extension building infrastructure.
cd pgaudit
make USE_PGXS=1
sudo make USE_PGXS=1 install
If all goes well, the compiled extension files will be copied to the correct PostgreSQL directory. â¨
Waking the Eye: Configuring pgAudit
Installing the files isn’t enough. We have to tell PostgreSQL to actually load the extension when it starts up. This requires a small but critical change. You can do this in one of two ways.
Method 1: Directly Editing postgresql.conf
(The Old-Fashioned Way)
The location of the postgresql.conf
file can vary. On Debian-based systems, it’s often at /etc/postgresql/16/main/postgresql.conf
. The surest way to find it on any system is to connect to psql
and run SHOW config_file;
. Once you’ve found it, open the file and find the shared_preload_libraries
parameter. Add pgaudit
to the comma-separated list.
shared_preload_libraries = 'pg_stat_statements,pgaudit'
Method 2: Using ALTER SYSTEM
(The Modern Way)
Alternatively, you can set this parameter directly from psql
using the ALTER SYSTEM
command. This is often safer as it avoids manual syntax errors. First, it’s wise to check the current value:
SHOW shared_preload_libraries;
Then, append pgaudit
to the existing value. For example, if the output was 'pg_stat_statements'
, you would run:
ALTER SYSTEM SET shared_preload_libraries = pg_stat_statements,pgaudit;
This command edits the postgresql.auto.conf
file, which overrides postgresql.conf
. It’s a cleaner way to manage configuration changes made via SQL.
The Unavoidable Restart
â ď¸ CRITICAL STEP: Regardless of which method you choose, this change requires a full restart of the PostgreSQL service. A simple reload (pg_ctl reload
) will not work. pgAudit
needs to hook into the server process at startup, and PostgreSQL is very particular about this, like a Sith Lord about protocol. Do it wrong, and it will Force-choke your startup process.
The exact service name may vary depending on your operating system and how you installed PostgreSQL. It could be version-specific (like postgresql-16
) or generic (like postgresql
).
# Use the service name appropriate for your system, e.g., postgresql or postgresql-16
sudo systemctl restart postgresql
sudo systemctl status postgresql
One Extension to Rule Them All: Enabling the pgAudit Extension in Your Database
The final step is to enable the extension within the specific database(s) you want to audit. Loading the library just makes it available; this command creates the necessary functions and objects for pgAudit
to work inside a database.
Connect to your target database using psql
or your favorite client:
psql -d my_precious_database
And run the following command:
CREATE EXTENSION pgaudit;
You can verify that the extension was installed correctly by using the \dx
command in psql
, which lists all installed extensions in the current database.
\dx
You should see pgaudit
in the list. With that, the Eye is open. Itâs not watching anything specific yetâwe haven’t given it any instructionsâbut it’s installed, loaded, and ready for our command.
What’s Next?
Weâve successfully laid the foundation. The pgAudit
extension is now a part of our PostgreSQL instance, ready to serve.
In Part 2 of this series, we’ll teach the Eye what to watch. We will dive into Session Auditing, configuring pgAudit
to log entire classes of statements like READ
, WRITE
, and DDL
, and we’ll discuss the crucial settings that keep your logs clean and your passwords out of them.