The error log was bleeding email addresses across the console. Every query, every audit trail—plain text, exposed. You knew it the second you scrolled. This is the kind of leak that turns a debug session into a security incident.
Masking email addresses in logs with Pgcli is not optional. It is the line between safe logging and an exploitable data dump. Pgcli, the Postgres command-line tool with smart autocompletion and syntax highlighting, will happily echo results from your queries, including sensitive fields like user.email. To protect privacy and comply with policies like GDPR or SOC 2, those values must be masked before they leave the database or land in a log file.
Start by rewriting your queries to replace email output with a masked representation. A common pattern is:
SELECT regexp_replace(email, '([^@]{3})[^@]*(?=@)', '\1***') AS email_masked
FROM users;
This keeps part of the local name and scrubs the rest. Pgcli will display the obfuscated value instantly on execution. No raw address ever surfaces in your logs.
For application-level logging, intercept query responses before printing. In Python with psycopg2, hook your result formatting step: