Skip to content

Commit 34e5ad1

Browse files
adela-bytebaseAdela
andauthored
docs: add pg best practice (#587)
Co-authored-by: Adela <[email protected]>
1 parent 051c134 commit 34e5ad1

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

content/reference/postgres/how-to/_layout.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
## [Top psql commands with examples](/reference/postgres/how-to/top-psql-commands-with-examples)
1313

14+
## [Postgres Security Best Practices](/reference/postgres/how-to/postgres-security-best-practices)
15+
1416
## [How to manage users and roles](/reference/postgres/how-to/how-to-manage-postgres-users-and-roles)
1517

1618
## [How to check Postgres version](/reference/postgres/how-to/how-to-check-postgres-version)
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
---
2+
title: Postgres Security Best Practices
3+
updated_at: 2025/04/09 12:00:00
4+
---
5+
6+
PostgreSQL powers critical applications worldwide — but insecure deployments risk data breaches, financial penalties, and reputational harm. This guide delivers actionable strategies to safeguard your database against threats like unauthorized access, data leaks, misconfigurations, and SQL injection. Whether you’re a developer, DBA, or DevOps professional, use these best practices to harden your PostgreSQL environment.
7+
8+
## 1. Secure Installation and Initial Configuration
9+
10+
**Minimal Installation:** Only install the extensions and tools you need. Unused packages can increase the attack surface.
11+
12+
**Keep PostgreSQL Updated:** Use the latest stable release to benefit from security patches. Subscribe to PostgreSQL security announcements.
13+
14+
**Initial Hardening Checklist:**
15+
16+
- Change the default `postgres` superuser password immediately after installation.
17+
- Set `listen_addresses` in `postgresql.conf` to specific IPs instead of `*`.
18+
- Disable the `trust` method in `pg_hba.conf`, especially in production.
19+
- Revoke unnecessary privileges from the `public` schema.
20+
21+
## 2. Authentication Best Practices??? better name?
22+
23+
**Role and User Management:**
24+
25+
- Apply the principle of least privilege.
26+
- Avoid shared accounts; create unique roles for each user and application.
27+
28+
**Password Policies:**
29+
30+
- Use strong, complex passwords.
31+
- Store passwords using SCRAM-SHA-256 rather than MD5.
32+
- Integrate with LDAP or PAM for centralized authentication.
33+
34+
**Two-Factor Authentication (2FA):**
35+
36+
While PostgreSQL doesn’t natively support 2FA, you can implement it at the network or OS layer using SSH, VPN, or identity providers.
37+
38+
**Restrict Superuser Access:**
39+
40+
- Use the `postgres` role only for critical maintenance.
41+
- Monitor all superuser activity.
42+
43+
## 3. Authorization and Access Control
44+
45+
**Role-Based Access Control (RBAC):**
46+
47+
- Use `GRANT` and `REVOKE` to assign only necessary permissions.
48+
- Organize roles into groups for easier management.
49+
50+
**Schema and Table Permissions:**
51+
52+
- Lock down access to sensitive tables with `REVOKE`.
53+
- Use `SECURITY DEFINER` functions with caution and never as superuser.
54+
55+
**Row-Level Security (RLS):**
56+
57+
- Implement RLS to enforce per-user or per-tenant access policies.
58+
- Use `CREATE POLICY` and `ALTER TABLE ENABLE ROW LEVEL SECURITY`.
59+
60+
**Public Schema:**
61+
62+
- Revoke default access with: `REVOKE ALL ON SCHEMA public FROM public;`
63+
64+
## 4. Data Encryption
65+
66+
**Encryption in Transit:**
67+
68+
- Enable SSL/TLS in `postgresql.conf`: `ssl = on`
69+
- Require SSL for clients: `sslmode=require`
70+
- Rotate certificates regularly.
71+
72+
**Encryption at Rest:**
73+
74+
- Use OS-level encryption (e.g., LUKS, EBS encryption).
75+
- Consider PostgreSQL extensions like `pgcrypto` for column-level encryption.
76+
- Evaluate third-party Transparent Data Encryption (TDE) solutions if compliance requires it.
77+
78+
## 5. Network Security
79+
80+
**Restrict Access:**
81+
82+
- Use firewall rules or security groups to allow only trusted IP ranges.
83+
- Never expose PostgreSQL directly to the public internet.
84+
85+
**Configure pg_hba.conf Carefully:**
86+
87+
- Prefer `scram-sha-256` or `md5`, never trust in production.
88+
- Limit IP ranges per user or role.
89+
90+
**Additional Hardening:**
91+
92+
- Use a VPN or SSH tunnel for remote access.
93+
- Change the default port (5432) to reduce visibility to automated scans.
94+
95+
## 6. Auditing and Monitoring
96+
97+
**Enable Detailed Logging:**
98+
99+
- `log_connections = on`
100+
- `log_disconnections = on`
101+
- `log_statement = 'ddl'`
102+
- `log_duration = on`
103+
104+
**Use Audit Tools:**
105+
106+
- Install `pgAudit` for fine-grained auditing.
107+
- Export logs to centralized systems (e.g., ELK, Splunk).
108+
109+
**Intrusion Detection:**
110+
111+
- Monitor for failed login attempts and role escalations.
112+
- Set up alerts for suspicious activity.
113+
114+
## 7. Patching and Maintenance
115+
116+
**Apply Security Updates Promptly:**
117+
118+
- Use automated patch management when available.
119+
- Test patches in staging environments.
120+
121+
**Stay Informed:**
122+
123+
- Subscribe to `pgsql-announce` for security updates.
124+
- Monitor CVEs related to PostgreSQL and dependencies.
125+
126+
## 8. Backup and Disaster Recovery
127+
128+
**Encrypted Backups:**
129+
130+
- Use `pg_dump` or base backups with encrypted storage.
131+
- Protect backup access credentials.
132+
133+
**Restore Testing:**
134+
135+
- Regularly test your restore process.
136+
- Automate backup integrity checks.
137+
138+
**Disaster Recovery Planning:**
139+
140+
- Define RTO (Recovery Time Objective) and RPO (Recovery Point Objective).
141+
- Store backups offsite and use redundant storage solutions.
142+
143+
## 9. Advanced Security Techniques
144+
145+
**OS-Level Protections:**
146+
147+
- Use AppArmor or SELinux to restrict PostgreSQL process capabilities.
148+
149+
**Connection Throttling:**
150+
151+
- Deploy `pgbouncer` to pool and limit abusive connections.
152+
153+
**Security Extensions:**
154+
155+
- Leverage `pgcrypto` for encryption
156+
- Consider `sepgsql` for mandatory access control
157+
158+
## 10. Common Mistakes and Vulnerabilities
159+
160+
- Using trust authentication in production.
161+
- Leaving the postgres role with default settings.
162+
- Failing to restrict access to the public schema.
163+
- Not using parameterized queries (risk of SQL injection).
164+
- Ignoring patch announcements.
165+
166+
## Summary
167+
168+
Securing PostgreSQL isn’t a one-time job. Keep working at it. Use multiple security steps and check your setup often to protect your data, keep it private, and make sure it’s always there when you need it.
169+
170+
New dangers pop up over time. Change your security plans as needed. Always update your tools, stay alert for problems, and treat your database like an important part of your work.

0 commit comments

Comments
 (0)