TMAegis
More rules for postgres

TMA-POSTGRES-3.1.20

Ensure 'log_connections' is enabled

MediumLevel 1Automatedpostgres

Description

Enabling the log_connections setting causes each attempted connection to the server to be logged, as well as successful completion of client authentication. This parameter cannot be changed after the session start. PostgreSQL 18 turned log_connections into a list; 'all' (or receipt,authentication,authorization,setup_durations) passes, the pre-18 boolean 'on' does not. Runs psql as ${PGUSER:-postgres} against ${PGDATABASE:-postgres}; set PGHOST/PGPORT and use ~/.pgpass (not PGPASSWORD, see 1.6/1.7) when peer authentication is not available.

How it is checked

Audit method: SQL query

sql_query
out=$(psql -X -U "${PGUSER:-postgres}" -d "${PGDATABASE:-postgres}" -tAc "SELECT CASE WHEN current_setting('log_connections') = 'all' OR string_to_array(replace(current_setting('log_connections'), ' ', ''), ',') @> ARRAY['receipt','authentication','authorization','setup_durations'] THEN 'compliant' ELSE 'noncompliant' END" 2>/dev/null) || exit 1; printf '%s\n' "$out"
Expected output
compliant

How it is fixed

Fix method: SQL statement

shell
psql -X -U "${PGUSER:-postgres}" -c "ALTER SYSTEM SET log_connections = 'all'" -c "SELECT pg_reload_conf()"

Properties

Operating systems
Any
Tags
postgresdatabaselogging
Reference
CIS PostgreSQL 18 Benchmark v1.0.0 - 3.1.20 Ensure 'log_connections' is enabled

Check this host

Run this on a machine you are allowed to audit. The scan is read-only.

shell
tmaegis scan --target postgres