Goal: require TLS for one application role, prove both paths, and restore the baseline.
The setup created app_svc with the lesson-only SCRAM password and saved HBA to /tmp/tls-hba-original.conf. Connect with -h /tmp: a Unix-socket directory, intentionally outside TCP/TLS tests. -U postgres chooses the SQL administrator and -d beer_db the database. \pset null labels missing fields without changing stored values. Expect beer_db=# and the display confirmation.
Goal: inspect the starting TLS setting and its supplied files.
SHOW must report ssl off. \! runs a shell command from psql; as-postgres selects the Linux cluster owner, separately from the SQL role. ls -al lists the certificate and private key; the key is owner-only. These are already provisioned lab files, not files you must generate. Their presence alone does not prove a live TLS connection.
Goal: enable the runtime TLS setting on this capable build.
ALTER SYSTEM writes the persistent ssl override; reload returns t for signal sent. pg_sleep(0.2) allows processing and prints a blank result cell. SHOW must then report on. PostgreSQL’s TLS capability was compiled in; ssl is also a real runtime setting, not merely a compile-time feature.
Goal: verify the library behind the runtime capability.
Expect ssl_library = OpenSSL. This names the compiled TLS library, not an encryption report for every client. The next tests establish actual TCP encryption.
Goal: put the encrypted path before a narrow plaintext denial.
sed -i edits the HBA file; 1i inserts at its start and \n separates the two lines. The fields name transport, beer_db, app_svc, one IPv4 address (/32), and authentication method. TLS matches hostssl/SCRAM first; plaintext skips it and matches host/reject. Reload, wait, then expect those two parsed rows in rule order. ANY(user_name) checks the role array. File rows alone do not prove the rules were loaded.
Goal: prove this role cannot use a plaintext TCP connection.
The child psql command uses sslmode=disable, the correct password, and IPv4 loopback. PGPASSWORD is scoped to that command; PGCONNECT_TIMEOUT=3 bounds connection establishment, -w avoids a password prompt, and -c runs SQL then exits. Expect pg_hba.conf rejects connection and no encryption. \! returns you to the still-open administrator.
Goal: prove the otherwise matching application connection actually uses TLS.
Repeat the child connection with sslmode=require. The query selects its own pg_stat_ssl row via pid = pg_backend_pid(), so another backend cannot supply evidence. Expect current_user = app_svc and ssl = t. Encryption is proven; server identity verification is separate. The lab uses a self-signed localhost certificate.
Goal: read the encryption-reporting view’s columns.
\d pg_stat_ssl describes a view; it does not list active connections. Expect pid, ssl, version, cipher, bits, and certificate fields. NULL certificate fields need not mean a failed TLS connection; client certificates are optional for our password-authenticated test.
Goal: explain the administrator’s own transport honestly.
Join the views by backend PID and filter to pg_backend_pid(). Expect postgres, client_addr (not applicable), and ssl f: this client explicitly used a Unix socket. The app_svc child connection already exited, so it should not appear as an active row. Omitting -h would not ensure a socket here because PGHOST defaults to localhost.
Goal: restore the initial policy and verify a fresh plaintext login works again.
The setup saved the original HBA file. cp restores it; ALTER SYSTEM RESET ssl removes our auto-file override, then reload and the brief wait apply both changes. Expect ssl = off, zero app-specific rules, and app_svc from a new no-password plaintext connection under the restored trust baseline. We restore this intentionally permissive lab baseline; it is not a production recommendation.