Real-World PostgreSQL Permission Matrix
Role | Access | Purpose |
---|
readonly | SELECT on all tables | BI/reporting, analysts |
readwrite | SELECT, INSERT, UPDATE | App developers, staging users |
admin | ALL PRIVILEGES on DB | Senior DBAs, migration scripts |
auditor | SELECT + pg_stat* views | Auditing, compliance, logs |
🧪 Hands-On Mini Project: Secure a Real Table
Now:
-
hr_user
can accessemployee_salary
. -
dev_user
getspermission denied
. 🔐 5. Role & Privilege Management
CREATE ROLE app_user LOGIN PASSWORD 'securepass';
GRANT CONNECT ON DATABASE mydb TO app_user;
🔐 6. Auditing and Logging
Enable and configure logs in
postgresql.conf
:1. 🏛️ Database-Level Security
2. 📂 Schema-Level Security
Controls access to objects (tables, views, functions, etc.) in a schema.
Key Concepts:
-
Use
GRANT USAGE
for schema access. -
Use
GRANT CREATE
to allow object creation.
3. 🧱 Table-Level Security
Controls access to entire tables for SELECT, INSERT, UPDATE, DELETE.
Key Concepts:
-
Use
GRANT
/REVOKE
on specific privileges. -- Allow read-only access
GRANT SELECT ON employees TO analyst_user;
-- Allow full access
GRANT SELECT, INSERT, UPDATE, DELETE ON employees TO app_user;
-- Revoke access
REVOKE ALL ON employees FROM intern_user;
4. 📊 Column-Level Security
Controls which columns a user can SELECT
or UPDATE
.
Key Concepts:
-
PostgreSQL supports
GRANT
andREVOKE
at the column level. -- Grant access only to name and department
GRANT SELECT (name, department) ON employees TO analyst_user;
-- Allow update to only the salary column
GRANT UPDATE (salary) ON employees TO hr_manager;
No comments:
Post a Comment