Goal This query acts as a global security alert. It triggers a warning if no roles in the entire database have explicitly revoked the default PUBLIC execute privileges for newly created routines. In PostgreSQL, new functions and procedures are executable by anyone (PUBLIC) by default. Leaving this unrestricted database-wide violates the principle of least privilege, potentially exposing sensitive business logic or data manipulation routines to unauthorized database users.
Type Problem detection Each row in the result could represent a flaw in the design
Reliability Medium Medium number of false-positive results
License MIT (opens in new tab)
Data Source system catalog only
SQL Query
WITH roles AS (SELECT r.rolname
FROM pg_default_acl d
JOIN pg_roles r ON r.oid = d.defaclrole
WHERE d.defaclobjtype = 'f' 
  AND d.defaclnamespace = 0 
  AND NOT EXISTS (
      SELECT 1 
      FROM unnest(d.defaclacl) AS acl 
      WHERE acl::text LIKE '=%'
  ))
SELECT 'All roles have a default privilege to create PUBLIC routines in this database' AS problem_description
WHERE NOT EXISTS (SELECT * FROM roles);

Fixing Suggestion

To resolve this security vulnerability and enforce the principle of least privilege, the default public execution rights must be revoked. For the roles that create routines (or globally, if applicable), execute the following command: ALTER DEFAULT PRIVILEGES REVOKE EXECUTE ON ROUTINES FROM PUBLIC;. Once this baseline security is established, you must explicitly GRANT EXECUTE only to the specific roles or users that genuinely require access to those routines.

Fixing Actions

SQL statements that help generate fixes for the identified problem.

Fix Action #1
SELECT 'ALTER DEFAULT PRIVILEGES REVOKE EXECUTE ON ROUTINES FROM PUBLIC;' AS sql_statement;

Revoke the default privilege from the current role.

# SQL Query to Generate Fix Description
1
SELECT 'ALTER DEFAULT PRIVILEGES REVOKE EXECUTE ON ROUTINES FROM PUBLIC;' AS sql_statement;
Revoke the default privilege from the current role.

Collections

This query belongs to the following collections:

Find problems automatically

Queries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .

NameDescription
Find problems automaticallyQueries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .

Categories

This query is classified under the following categories:

Security

Queries of this category provide information about the security measures.

User-defined routines

Queries of this category provide information about the user-defined routines

NameDescription
SecurityQueries of this category provide information about the security measures.
User-defined routinesQueries of this category provide information about the user-defined routines