Database Security
Table of Contents
Introduction: The Security Audit That Humbled Me
PostgreSQL Authentication and pg_hba.conf
pg_hba.conf Format
Method
Security
Notes
Reload After Changes
Roles and Privileges: The Foundation of Access Control
Role Hierarchy for a Blog Application
Creating Roles and Users
Principle of Least Privilege in Practice
Column-Level Privileges
Revoking Privileges
Row-Level Security (RLS)
Setting Application Context
Multi-Tenant with RLS
SQL Injection: The Most Common Attack
The Attack
Prevention: Always Use Parameterised Queries
Defence in Depth
Encryption: Data at Rest and in Transit
SSL/TLS (Encryption in Transit)
Encrypting Sensitive Columns (pgcrypto)
Transparent Data Encryption
Auditing and Monitoring
pg_audit Extension
Custom Audit Triggers
Monitor Failed Login Attempts
Connection Security
Connection Limits
Enforce Password Policies
Protect Against Brute Force
Secrets Management
Never Hardcode Credentials
Use a .env File (Development Only)
.env File (Development Only)Production: Use Secret Managers
Security Checklist for Production
What I Learned About Database Security
Next Steps
Last updated