CodeGym /Courses /SQL SELF /Access Audit and User Action Logging

Access Audit and User Action Logging

SQL SELF
Level 48 , Lesson 0
Available

Imagine your database is like a club with super strict membership rules. Only the right people can get in, but we want to know who came, when, how long they stayed, and what they did. That's exactly what auditing in PostgreSQL is for. With it, you can:

  1. Track user actions. For example, who ran an important query and when.

  2. Spot suspicious activity. Like someone trying to read data they shouldn't have access to.

  3. Comply with laws and standards. In a lot of industries (like finance or healthcare), you gotta keep a detailed audit of user actions.

  4. Understand your system from the inside. Logging helps you figure out which queries run most often, where the bottlenecks are, and how to optimize performance.

Setting Up Logging Parameters

PostgreSQL lets you set up auditing with config parameters like log_statement and log_connections. Let's break them down.

  1. log_statement: what do we log?

The log_statement parameter decides which SQL queries get written to the logs. Super useful for understanding what's going on in your system.

Possible values for log_statement:

  • none — log nothing (great if you like living on the edge).
  • ddl — log only commands that change the database structure (like CREATE TABLE).
  • mod — log commands that change data (like INSERT, UPDATE, DELETE).
  • all — log literally everything.

Example setup: to change this parameter, you'll need to update the postgresql.conf file:

# Setting up logging for all SQL queries
log_statement = 'all'

After that, save your changes and restart the PostgreSQL server:

pg_ctl reload

Now your club will log every move, from ordering drinks to busting out dance moves.

  1. log_connections: who's coming into the club?

The log_connections parameter logs info about every new connection to the database. There's also log_disconnections, which logs when a connection closes.

Example setup: Again, update the postgresql.conf file:

# Logging connections
log_connections = on

# Logging disconnections
log_disconnections = on

What does this give us? For example, you can see in the logs that your manager spent two hours trying to connect with the wrong password. Yep, at 3am.

Log Analysis: What Can You Find?

Once you've set up log_statement and log_connections, PostgreSQL will start writing logs. Here's what a log file might look like with log_statement = 'mod':

2023-11-01 12:45:01 UTC [12345] LOG:  connection authorized: user=admin database=university
2023-11-01 12:46:15 UTC [12345] STATEMENT:  INSERT INTO students (name, age) VALUES ('Alice', 22);
2023-11-01 12:47:30 UTC [12345] STATEMENT:  UPDATE students SET age = 23 WHERE name = 'Alice';
2023-11-01 12:48:45 UTC [12345] LOG:  disconnection: session time: 2:45 connection: 1/5

Interesting Bits:

  1. Who connected: user=admin database=university — that's our boss.
  2. What they did: inserted a row with the name Alice and updated her age.
  3. When they left: 12:48:45, after two and a half minutes of action.

Practical Examples: How to Use Auditing in Real Life

Let's check out a few scenarios where auditing and logging can come in handy.

Scenario 1: Tracking Data Changes

You suspect someone is changing records in the students table. Here's how you can set up auditing:

  1. Set log_statement = 'mod' to log all modifying queries.
  2. Analyze the logs:
cat /var/log/postgresql/postgresql.log | grep "UPDATE students"

Now you can see who made what changes and when.

Scenario 2: Spotting Suspicious Connections

If you see lots of connections from different IPs in the logs, something might be up. To analyze, use:

  1. Connection logs (log_connections).
  2. Filter by IP addresses:
cat /var/log/postgresql/postgresql.log | grep "connection authorized"

Scenario 3: Query Performance Analysis

Want to know why your server is slow? An easy way is to turn on log_statement = 'all' for a short time (like an hour), collect the logs, and see where the server spends most of its time.

Best Practices

Don't log everything. Set up log_statement to only log important stuff — DDL and data changes.

Automate log analysis. Use scripts or tools to regularly read and analyze logs, like grep, awk, or even the ELK Stack (Elasticsearch, Logstash, Kibana).

Manage log size. Set up log rotation using PostgreSQL parameters or OS tools (like logrotate on Linux).

Hope you feel like a real sheriff of your database now. Logging and auditing aren't just about protection — they're an awesome way to really understand what's happening in your system. We'll see how this stuff works in real projects!

2
Task
SQL SELF, level 48, lesson 0
Locked
Enabling logging of all SQL queries
Enabling logging of all SQL queries
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION