TEC-DATA86-03 · Lesson 2 · 10 clock hours · theory
Core Configuration: postgresql.conf and pg_hba.conf
Objective. Students adjust key server parameters in postgresql.conf and write client authentication rules in pg_hba.conf, explaining the effect of each change.
⚠ Pending professional review
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
Core Configuration: postgresql.conf and pg_hba.conf
Why Configuration Files Matter
A freshly installed PostgreSQL server uses conservative default settings designed to run on modest hardware. In a production environment those defaults rarely match real workloads. Two files control the vast majority of server behavior: postgresql.conf sets performance, logging, and replication parameters; pg_hba.conf controls who may connect, from where, and how they must authenticate. Mastering both files is a foundational DBA skill tested by the EnterpriseDB PostgreSQL CE exam and expected by every employer who runs PostgreSQL.
Always keep a timestamped backup of both files before editing. A syntax error in either file can prevent the server from starting or accepting connections, which constitutes a production outage.
---
postgresql.conf — Server Configuration
postgresql.conf is a plain-text file located in the data directory (often `/etc/postgresql/<version>/main/` on Debian-based systems or `$PGDATA` on others). Parameters follow the format:
```
parameter_name = value # optional comment
```
Lines beginning with `#` are comments. The server reads this file at startup; many parameters can also be reloaded without a full restart using `pg_ctl reload` or the SQL command `SELECT pg_reload_conf();`. A smaller set of parameters (such as `max_connections`) require a full service restart because they affect shared-memory allocation.
Ten commonly tuned parameters:
| Parameter | What it controls | Reload or restart? |
|---|---|---|
| `shared_buffers` | Size of the shared memory buffer pool | Restart |
| `work_mem` | Memory per sort/hash operation per query node | Reload |
| `maintenance_work_mem` | Memory for VACUUM, CREATE INDEX, etc. | Reload |
| `max_connections` | Maximum simultaneous client connections | Restart |
| `effective_cache_size` | Planner estimate of OS cache available | Reload |
| `wal_level` | Amount of information written to WAL | Restart |
| `checkpoint_completion_target` | Fraction of checkpoint interval for writes | Reload |
| `logging_collector` | Enable file-based log capture | Restart |
| `log_min_duration_statement` | Log queries exceeding this duration (ms) | Reload |
| `max_wal_size` | Maximum WAL size before forced checkpoint | Reload |
shared_buffers is the single most impactful parameter. The PostgreSQL community recommends starting at roughly 25% of total RAM as a baseline, then tuning based on observed performance. On a server with 64 GB RAM that gives 16 GB. PostgreSQL also relies on the operating system's file-system cache, so allocating more than ~40% of RAM to shared_buffers is generally counterproductive.
work_mem is per sort or hash operation per query node, and a single complex query can allocate it multiple times. Set it conservatively: `Total RAM × a small fraction ÷ max_connections` is a useful starting heuristic. Too high a value with many concurrent users causes out-of-memory conditions.
After editing, confirm active values with `SHOW parameter_name;` or query `pg_settings`:
```sql
SELECT name, setting, unit FROM pg_settings
WHERE name IN ('shared_buffers','work_mem','max_connections');
```
---
pg_hba.conf — Client Authentication
pg_hba.conf (Host-Based Authentication) is read top-to-bottom; the first matching rule wins. Each rule is one line with five space-separated fields:
```
TYPE DATABASE USER ADDRESS METHOD [options]
```
TYPE values:
- `local` — Unix-domain socket connections (no ADDRESS field used)
- `host` — TCP/IP connections (plain or SSL)
- `hostssl` — TCP/IP, SSL required
- `hostnossl` — TCP/IP, SSL forbidden
Common authentication methods:
| Method | Description |
|---|---|
| `trust` | No password; accept unconditionally. Use only on loopback for tightly controlled setups. |
| `reject` | Always deny. Useful as an explicit block rule. |
| `ident` | Map OS username to PostgreSQL username via the ident protocol (local/host). |
| `peer` | Check OS username of the connecting process (local connections only). |
| `md5` | MD5-hashed password challenge. Widely supported; weaker than scram. |
| `scram-sha-256` | SCRAM-SHA-256 challenge-response. Current recommended method for password authentication. |
| `ldap` | Delegate authentication to an LDAP server. |
| `cert` | Require a valid client SSL certificate. |
A safe rule ordering for a typical production server places `reject` rules for sensitive databases near the top, followed by specific user/CIDR rules, and ends with a narrow default.
Example rules:
```
local all postgres peer
host appdb appuser 192.168.10.0/24 scram-sha-256
host legacydb legacyuser 10.0.5.50/32 md5
```
After editing pg_hba.conf, reload (no restart needed): `SELECT pg_reload_conf();`
---
Safety and Best Practices
- Version control your config files. Use git or a configuration management tool so every change is traceable.
- Never use `trust` for remote TCP/IP connections in any production or multi-user environment.
- Prefer `scram-sha-256` over `md5` for all new deployments; md5 is vulnerable to offline dictionary attacks.
- Test rules before going live. Attempt connections as the target user from the expected source IP to confirm the correct rule fires.
- Document every change in your configuration journal with the date, the before/after values, the reason, and the observed effect. This record is essential for audits and incident response.
- In shared server rooms, always follow site physical-access procedures; for server hardware maintenance, observe lockout/tagout (LOTO) procedures per OSHA standards before touching electrical components—though most DBA work is performed remotely, awareness of lab safety rules is required when working in hands-on lab environments.
Key terms8
postgresql.confThe primary server configuration file for PostgreSQL, containing parameters that control memory, connections, WAL behavior, logging, and more.
pg_hba.confHost-Based Authentication configuration file; each line defines a rule specifying which users may connect to which databases from which hosts using which authentication method.
shared_buffersA postgresql.conf parameter that sets the amount of memory PostgreSQL allocates for its shared buffer pool; a common starting point is 25% of total system RAM.
work_memMemory allocated per sort or hash operation per query execution node; applies multiple times within a single complex query, so it must be sized carefully relative to max_connections.
scram-sha-256The current recommended pg_hba.conf password authentication method, using SCRAM-SHA-256 challenge-response to avoid sending passwords over the network.
wal_levelA postgresql.conf parameter controlling how much information is written to the Write-Ahead Log; options include minimal, replica, and logical, with higher levels enabling replication and logical decoding.
pg_reload_conf()A PostgreSQL SQL function that signals the server to re-read configuration files without a full restart; effective for parameters that support reload.
CIDR notationA compact way to specify an IP address range in pg_hba.conf (e.g., 192.168.1.0/24 covers all addresses from 192.168.1.0 to 192.168.1.255).
Worked examples4
1. A production server has 64 GB of RAM. Calculate the recommended starting value for shared_buffers using the 25% community guideline, and express it in megabytes.
- Convert total RAM to megabytes: 64 GB × 1024 MB/GB = 65,536 MB
- Apply the 25% guideline: 65,536 MB × 0.25 = 16,384 MB
- Express in PostgreSQL notation: shared_buffers = 16384MB (or equivalently 16GB)
Answer: shared_buffers = 16384MB (16 GB)
2. On the same 64 GB server with max_connections = 200, estimate a conservative work_mem value using the heuristic: allocate 5% of RAM to the total work_mem budget, then divide by max_connections. Express the result in MB.
- Calculate 5% of total RAM in MB: 65,536 MB × 0.05 = 3,276.8 MB
- Divide by max_connections: 3,276.8 MB ÷ 200 = 16.384 MB
- Round down to a clean value safe for production: work_mem = 16MB
Answer: work_mem = 16MB (a conservative starting point; tune upward only after monitoring actual query memory usage)
3. Write a pg_hba.conf rule that allows the user 'analyst' to connect to the database 'reports' from any host in the subnet 10.10.20.0/24 using scram-sha-256 authentication.
- Identify the TYPE: TCP/IP connection, so use 'host'
- Fill in DATABASE: reports
- Fill in USER: analyst
- Fill in ADDRESS: 10.10.20.0/24
- Fill in METHOD: scram-sha-256
- Assemble the line: host reports analyst 10.10.20.0/24 scram-sha-256
Answer: host reports analyst 10.10.20.0/24 scram-sha-256
4. After editing postgresql.conf to set work_mem = 32MB, a DBA runs a SHOW command and still sees 16MB. What is the most likely cause, and what is the correct action?
- Check whether work_mem requires a restart or only a reload — it requires only a reload
- Determine whether a reload was performed after saving the file
- If no reload was done, execute: SELECT pg_reload_conf(); or run pg_ctl reload from the OS
- Run SHOW work_mem; again to confirm the new value is active
Answer: The server was not reloaded after the file was saved. Running SELECT pg_reload_conf(); and then SHOW work_mem; should confirm the updated value of 32MB.
The math in this lesson1
Learners convert 64 GB to MB and apply a 25% guideline to set shared_buffers.
7.RP.3
In class: Read PostgreSQL Documentation Chapter 20 (Client Authentication) and Chapter 19 (Server Configuration); annotate a printed excerpt identifying the ten most commonly tuned parameters (shared_buffers, work_mem, max_connections, wal_level, etc.) and the authentication methods available in pg_hba.conf. · Using a provided scenario card (e.g., 'production server with 64 GB RAM, 200 concurrent users'), calculate recommended values for shared_buffers (roughly 25% of RAM as a starting point per community guidance) and work_mem, enter them in postgresql.conf, reload the server, and confirm changes with SHOW statements. · Write three pg_hba.conf rules covering local ident, md5, and scram-sha-256 authentication for different client CIDRs; attempt connections as test users to verify each rule fires correctly, logging results in a configuration journal.
Materials: PostgreSQL 16 Documentation Chapters 19–20, pgtune.leopard.in.ua reference (for instructor demonstration only), psql client, configuration journal template
Check: Instructor reviews configuration journal for correct parameter syntax, valid pg_hba.conf rules, and SHOW output screenshots; pass = all three authentication scenarios work as intended AND journal entries explain reasoning.
Practice check8
Answer each question, then check it. Nothing is recorded; this is your own practice.
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.