Auditing PostgreSQL
A compromised PostgreSQL server is rarely the result of a flaw in the engine. The usual story combines three things: port 5432 reachable from where it should not be, a pg_hba.conf rule that accepts almost anything, and a role with more privileges than it needs. If that role is a superuser, the attacker does not just read the database: with COPY ... FROM PROGRAM they run commands on the server as the postgres system user. This card walks through the configuration reviewed in an authorized audit, from inside with a role that can read the catalog and from outside with what any client sees.
Responsibility model
Section titled “Responsibility model”On a self-hosted PostgreSQL, everything belongs to the customer. On a managed service (RDS, Cloud SQL, Azure, Supabase), the provider runs the operating system, patches and backups, and does not hand out a real superuser. Even so, the customer decides:
- who can connect, and from where;
- which method authenticates each connection;
- which roles exist and what they can do;
- which extensions and functions are installed;
- what gets logged.
flowchart LR
A["Application"] -->|"TLS + SCRAM"| H{"pg_hba.conf<br/>first matching rule"}
X["Internet"] -. "5432 open,<br/>trust or md5 rule" .-> H
H --> R["Role"]
R -->|"SUPERUSER or<br/>pg_execute_server_program"| P["COPY ... FROM PROGRAM<br/>commands on the server"]
R -->|"least privilege"| T[("Tables and schemas")]
T --- F["SECURITY DEFINER functions<br/>with a fixed search_path"]
Who gets in: pg_hba.conf
Section titled “Who gets in: pg_hba.conf”pg_hba.conf is read top to bottom and the first rule that matches the connection type, database, user and address is applied. A broad rule at the bottom does no harm if a stricter one matches first. A broad rule at the top overrides everything below it.
| Method | What it does | Verdict |
|---|---|---|
trust |
Accepts the connection without a password | Never on host connections |
password |
Clear-text password over the network | Never |
md5 |
MD5 hash challenge | Obsolete: PostgreSQL 18 deprecates it and it will be removed in a future release |
scram-sha-256 |
SCRAM challenge | The right method when using passwords |
cert |
Client certificate | Recommended for services |
peer |
Operating system user, local socket only | Recommended for local administration |
Besides the method:
hostsslinstead ofhost, so that only TLS connections are accepted;- specific databases and users in each rule, not
all all; - specific networks, not
0.0.0.0/0; listen_addresseslimited to the interfaces that are needed;- the port filtered by the firewall, without relying on
pg_hba.confalone.
The pg_hba_file_rules view lets you review the rules from SQL, with the line number and an error field if a rule is malformed.
What each role can do
Section titled “What each role can do”| Attribute or role | Risk |
|---|---|
SUPERUSER |
Bypasses every permission. Can read and write server files and run programs with COPY ... FROM PROGRAM |
CREATEROLE |
Creates roles and, in older versions, can grant itself almost anything |
BYPASSRLS |
Ignores row-level security policies |
pg_execute_server_program |
Runs system programs with COPY ... FROM PROGRAM without being a superuser |
pg_read_server_files, pg_write_server_files |
Read or write any file the postgres user can reach |
Privileges for PUBLIC |
Held by every role, including the ones created tomorrow |
Since PostgreSQL 15, PUBLIC can no longer create objects in the public schema of new databases. Databases created with older versions and upgraded keep the old permission until someone revokes it.
SECURITY DEFINER functions. They run with their owner’s privileges. Without a fixed search_path, anyone who can create objects in a schema earlier in the path can replace a function or operator they use. If they also build SQL from strings, they are an SQL injection with someone else’s privileges (see SQL injection).
Lab: catalog audit
Section titled “Lab: catalog audit”A local PostgreSQL 16.15 (Ubuntu 24.04 package), listening on localhost only, with deliberate mistakes:
trustandmd5rules from any address;- passwords stored as MD5;
- an application role with
SUPERUSER; - a reports role with
pg_execute_server_programandpg_read_server_files; - a table with every privilege granted to
PUBLIC; - a
SECURITY DEFINERfunction withoutsearch_paththat concatenates SQL.
The audit is a read-only script over the catalog:
\pset footer off\echo '== 1. Roles with dangerous attributes'SELECT rolname, rolsuper AS super, rolcreaterole AS createrole, rolbypassrls AS bypassrls, rolcanlogin AS loginFROM pg_rolesWHERE (rolsuper OR rolcreaterole OR rolbypassrls) AND rolname NOT LIKE 'pg\_%'ORDER BY 1;
\echo '== 2. Predefined roles that read files or run programs'SELECT r.rolname AS role, m.rolname AS granted_toFROM pg_auth_members amJOIN pg_roles r ON r.oid = am.roleidJOIN pg_roles m ON m.oid = am.memberWHERE r.rolname IN ('pg_execute_server_program','pg_read_server_files','pg_write_server_files');
\echo '== 3. Passwords stored as MD5 (not SCRAM)'SELECT rolname FROM pg_authid WHERE rolpassword LIKE 'md5%';
\echo '== 4. pg_hba rules with trust or md5 from the network'SELECT line_number AS line, type, database, user_name, address, netmask, auth_methodFROM pg_hba_file_rulesWHERE auth_method IN ('trust','md5','password') AND type <> 'local';
\echo '== 5. Settings'SELECT name, setting FROM pg_settingsWHERE name IN ('listen_addresses','ssl','password_encryption','log_connections', 'log_disconnections','log_statement','shared_preload_libraries')ORDER BY 1;
\echo '== 6. Tables with privileges for PUBLIC (current database)'SELECT table_schema, table_name, string_agg(privilege_type, ',' ORDER BY privilege_type) AS privilegesFROM information_schema.role_table_grantsWHERE grantee = 'PUBLIC' AND table_schema NOT IN ('pg_catalog','information_schema')GROUP BY 1, 2;
\echo '== 7. SECURITY DEFINER functions without a fixed search_path'SELECT p.oid::regprocedure AS function, pg_get_userbyid(p.proowner) AS ownerFROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespaceWHERE p.prosecdef AND n.nspname NOT IN ('pg_catalog','information_schema') AND NOT EXISTS (SELECT 1 FROM unnest(coalesce(p.proconfig,'{}')) c WHERE c LIKE 'search_path=%');
\echo '== 8. Installed extensions'SELECT extname, extversion FROM pg_extension ORDER BY 1;Real output (trimmed) on the misconfigured instance:
== 1. Roles with dangerous attributes rolname | super | createrole | bypassrls | login app | t | f | f | t postgres | t | t | t | t== 2. Predefined roles that read files or run programs pg_read_server_files | reports pg_execute_server_program | reports== 3. Passwords stored as MD5 (not SCRAM) app reports== 4. pg_hba rules with trust or md5 from the network 3 | host | {all} | {all} | 0.0.0.0 | 0.0.0.0 | trust 4 | host | {all} | {all} | :: | :: | md5== 5. Settings log_connections | off password_encryption | md5 ssl | off== 6. Tables with privileges for PUBLIC (current database) public | customers | DELETE,INSERT,REFERENCES,SELECT,TRIGGER,TRUNCATE,UPDATE== 7. SECURITY DEFINER functions without a fixed search_path purge(text) | postgresThe fix, applied in the same lab:
ALTER SYSTEM SET password_encryption = 'scram-sha-256';ALTER SYSTEM SET log_connections = on;ALTER SYSTEM SET log_disconnections = on;SELECT pg_reload_conf();ALTER ROLE app NOSUPERUSER;REVOKE pg_execute_server_program, pg_read_server_files FROM reports;-- and in the shop database:REVOKE ALL ON customers FROM PUBLIC;GRANT SELECT ON customers TO reports;ALTER FUNCTION purge(text) SET search_path = pg_catalog, public;REVOKE EXECUTE ON FUNCTION purge(text) FROM PUBLIC;The passwords are then set again, and now stored with SCRAM. pg_hba.conf ends up like this:
local all postgres peer map=adminhostssl shop app,reports 10.0.0.0/24 scram-sha-256Running the audit again, sections 2, 3, 4, 6 and 7 come back empty and section 1 only lists postgres. Section 5 shows password_encryption at scram-sha-256 and connection logging on, but ssl is still off. The hostssl rule is useless until TLS is configured with a certificate (ssl = on, ssl_cert_file, ssl_key_file). The audit leaves it as an open finding.
From outside
Section titled “From outside”From outside, without credentials, you check:
- whether the port answers where it should not (see ports and Shodan);
- whether it offers TLS and with which certificate:
openssl s_client -starttls postgres -connect host:5432; - whether a connection without TLS is rejected:
psql "host=… sslmode=disable"must fail if every rule ishostssl.
A production PostgreSQL reachable from the internet is already a finding, even with good passwords.
Versions and patches
Section titled “Versions and patches”On November 12, 2026, PostgreSQL released 18.7, 17.12, 16.16, 15.20 and 14.25, together with the second release candidate of PostgreSQL 19. PostgreSQL 14 stops receiving fixes that same day. Minor releases fix vulnerabilities regularly; an instance several minor releases behind is a finding even if its major version is supported. The lab in this card uses 16.15 from the Ubuntu package, one minor release behind the published one.
Real-world cases
Section titled “Real-world cases”- PG_MEM and JINX-0126 (2024-2025). In August 2024, Aqua Nautilus described a campaign that brute-forced internet-facing PostgreSQL servers, created an admin role and used
COPY ... FROM PROGRAMto download a cryptocurrency miner. In April 2025, Wiz reported a variant with more than 1,500 victims. A weak password plus an exposed superuser was enough. - CVE-2025-1094 and BeyondTrust (February 2025). Rapid7 found that the
libpqescaping functions did not neutralize quotes inside text with invalid UTF-8, which allowed SQL injection when that text reachedpsql. In every scenario Rapid7 tested, the BeyondTrust Remote Support exploit (CVE-2024-12356) needed this flaw to reach code execution. Lesson: update clients and libraries too, not just the server.
Checklist
Section titled “Checklist”- Port 5432 not exposed to the internet;
listen_addresseslimited; firewall in front -
pg_hba.confwithouttrustorpasswordon network connections; nomd5;hostsslrules with specific databases, users and networks - TLS on with a valid certificate;
sslmode=verify-fullon clients -
password_encryption = scram-sha-256and no MD5 passwords left inpg_authid - No application role with
SUPERUSER,CREATEROLEorBYPASSRLS -
pg_execute_server_program,pg_read_server_filesandpg_write_server_filesnot granted - No privileges for
PUBLICon tables;CREATEonpublicrevoked in old databases -
SECURITY DEFINERfunctions with a fixedsearch_pathandEXECUTErevoked fromPUBLIC - Extensions reviewed; only the ones needed
-
log_connectionsandlog_disconnectionson; logs shipped to a SIEM - Supported major version with the latest minor release, on clients and
libpqas well
References
Section titled “References”- PostgreSQL: The pg_hba.conf File
- PostgreSQL: Predefined Roles
- PostgreSQL: Writing SECURITY DEFINER Functions Safely
- PostgreSQL 18 Released
- PostgreSQL 18.7, 17.12, 16.16, 15.20, 14.25 and 19 RC 2 Released
- PostgreSQL: Security Information
- Aqua Nautilus: PG_MEM, a malware hidden in the Postgres processes
- Rapid7: CVE-2025-1094, PostgreSQL psql SQL injection