🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Data Management / Database Administration › Database Security & Role Management › Lesson 2
TEC-DATA86-05 · Lesson 2 · 12 clock hours · lab

Authentication: pg_hba.conf, Password Policies & Scram-SHA-256

Objective. Learners configure PostgreSQL client authentication using multiple methods (trust, md5, scram-sha-256, peer) by editing pg_hba.conf and verify that only authorized connections succeed.

⚠ 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.

What Is pg_hba.conf?

PostgreSQL controls who may connect to the server through a file called pg_hba.conf (Host-Based Authentication configuration). Every incoming connection is checked against this file from top to bottom; the first matching record wins. If no record matches, the connection is refused. Understanding this file is the single most important step in locking down a PostgreSQL installation.

The default location on Debian/Ubuntu-based systems is `/etc/postgresql/16/main/pg_hba.conf`. On Red Hat-based systems it is typically inside the data directory, often `/var/lib/pgsql/16/data/pg_hba.conf`.

Record Format

Each non-comment line in pg_hba.conf has five fields:

```

TYPE DATABASE USER ADDRESS METHOD

```

Blank lines and lines beginning with `#` are ignored.

Authentication Methods

trust

Accepts the connection with no password. Use only on tightly controlled local sockets where the OS already restricts access, or in a disposable development container. Never use trust over a network interface in production.

peer

Applicable only to `local` (Unix-socket) connections. PostgreSQL asks the OS for the connecting process's Unix username and compares it to the requested database role. This is safe for DBA logins on the same machine because OS-level user management enforces identity.

md5

The client sends an MD5-hashed password. MD5 is still widely supported but is considered cryptographically weak. Prefer scram-sha-256 for all new deployments.

scram-sha-256

Introduced in PostgreSQL 10 and the recommended method as of PostgreSQL 16. Uses the Salted Challenge Response Authentication Mechanism (SCRAM) with SHA-256. The password is never transmitted in plain text, and replay attacks are prevented by a server-generated nonce. Passwords stored in `pg_authid` are hashed using SCRAM, making a database dump far less dangerous than one containing MD5 hashes.

reject

Immediately refuses the connection with an error. Placing a `reject` line above a broader `host all all 0.0.0.0/0` rule is a common pattern for blocking specific hosts or users while still allowing others.

Editing and Reloading

Always make a backup before editing:

```bash

sudo cp /etc/postgresql/16/main/pg_hba.conf \

/etc/postgresql/16/main/pg_hba.conf.bak

```

After editing, check your syntax mentally, then reload without a full restart:

```bash

sudo pg_ctlcluster 16 main reload

```

On Red Hat systems: `sudo systemctl reload postgresql-16`

A reload re-reads pg_hba.conf and `postgresql.conf` without dropping existing connections. New connections immediately use the updated rules.

Verifying Configuration

Inside psql, two tools help you confirm what is active:

Password Policies and scram-sha-256 in Practice

Creating a user with scram-sha-256 storage requires that the server parameter `password_encryption` is set to `scram-sha-256` (the default in PostgreSQL 16) before you issue `CREATE ROLE` or `\password`. Verify with:

```sql

SHOW password_encryption;

```

You can also set it per session:

```sql

SET password_encryption = 'scram-sha-256';

```

Always set a strong password and, where policy requires it, combine PostgreSQL authentication with network-layer controls such as firewalls and TLS certificates.

Cross-Platform Perspective

Oracle Database enforces password complexity, reuse limits, and expiration through password profiles assigned to users (`CREATE PROFILE … LIMIT PASSWORD_LIFE_TIME …`). Microsoft Azure SQL Database offers two authentication modes: SQL authentication (username/password stored in the database) and Microsoft Entra ID authentication (formerly Azure Active Directory), which allows centralized identity management and multi-factor authentication. PostgreSQL relies on pg_hba.conf combined with OS controls and, for enterprise deployments, LDAP or GSSAPI integration. All three platforms share the underlying goal: verify identity before granting database access.

Safety Notes

Key terms8

pg_hba.confThe Host-Based Authentication configuration file that PostgreSQL reads to determine which connections are allowed, which users may connect, and by what authentication method.
scram-sha-256A challenge-response authentication mechanism using SHA-256 hashing that prevents passwords from being transmitted in plain text and resists replay attacks; the recommended authentication method in PostgreSQL 16.
peer authenticationA Unix-socket authentication method where PostgreSQL asks the operating system for the connecting process's username and matches it to the requested database role, without requiring a password.
trustA pg_hba.conf authentication method that accepts a connection without any credential check; appropriate only for tightly controlled local sockets in development environments.
rejectA pg_hba.conf authentication method that immediately refuses a connection; commonly placed above permissive rules to block specific hosts or users.
pg_hba_file_rulesA PostgreSQL system view that displays the currently loaded and parsed contents of pg_hba.conf, allowing administrators to verify active authentication rules without reading the raw file.
password_encryptionA PostgreSQL server parameter that controls the hashing algorithm used when storing new passwords; should be set to scram-sha-256 in PostgreSQL 16 deployments.
hostsslA pg_hba.conf connection type that accepts only TCP/IP connections protected by TLS/SSL, combining encrypted transport with the specified authentication method.
In class: Instructor demo: show a default pg_hba.conf, explain record format (type, database, user, address, method), then learners edit a practice file to allow scram-sha-256 for a named application user and reject all others; reload PostgreSQL with pg_ctlcluster and test with psql. · Lab exercise: create three database users with different authentication requirements (local peer for a DBA, scram-sha-256 over TCP for an app account, and a deliberately rejected remote login); use \\conninfo and pg_hba_file_rules view to verify configuration. · Compare and contrast activity: learners read the Oracle Database Security Guide section on password profiles and the Microsoft DP-300 study guide section on Azure SQL authentication (Entra ID vs. SQL auth); fill in a three-column comparison table (PostgreSQL / Oracle / Azure SQL) for each authentication concept.
Materials: PostgreSQL 16 documentation (postgresql.org/docs), Oracle Database Security Guide (docs.oracle.com), Microsoft DP-300 Study Guide, Linux VMs with PostgreSQL 16 installed (one per learner or pair), pg_hba.conf template handout
Check: Learner demonstrates a working pg_hba.conf where: (1) a local DBA connects via peer, (2) an app user connects via scram-sha-256, and (3) an anonymous remote connection is refused; all three outcomes verified by instructor observation. Pass = all three verified correctly.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

PostgreSQL CE — server configuration and authentication
1. In pg_hba.conf, which field determines whether a record applies to a Unix-domain socket connection versus a TCP/IP connection?
PostgreSQL CE — server configuration and authentication
2. A PostgreSQL 16 server has the following pg_hba.conf lines in order: host all app_user 10.0.0.5/32 scram-sha-256 host all all 0.0.0.0/0 reject A user named app_user connects from 10.0.0.5. What happens?
PostgreSQL CE — server configuration and authentication
3. Which pg_hba.conf authentication method is appropriate for a local DBA account that connects via a Unix-domain socket on the same server, relying on the operating system's user identity?
PostgreSQL CE — security hardening
4. Why is scram-sha-256 preferred over md5 for TCP/IP connections in PostgreSQL 16?
PostgreSQL CE — server administration
5. After editing pg_hba.conf on a Debian-based system running PostgreSQL 16, which command applies the new rules to active connections without restarting the server?
PostgreSQL CE — monitoring and verification
6. Which PostgreSQL system view can an administrator query inside psql to confirm the currently active, parsed authentication rules from pg_hba.conf?
Oracle DBA CA — database security
7. In Oracle Database, a DBA wants to enforce password expiration and minimum password length for application accounts. Which Oracle object is used to define these constraints?
Microsoft DP-300 — Azure SQL security and identity
8. For the Microsoft DP-300 exam, a database administrator is configuring Azure SQL Database to allow developers to authenticate using their corporate Microsoft identity without storing separate database passwords. Which authentication mode should be enabled?
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.