PostgreSQL Error 2F004: Read SQL Data Not Permitted
In this article
The PostgreSQL error 2F004 (read_sql_data_not_permitted) means the current role has been explicitly denied the right to read SQL data through a function or procedure that requires it. The fix is to either grant the necessary privilege or change the function's security context. You cannot simply ignore it: the error is raised by PostgreSQL's internal privilege checks, so you must adjust permissions or the function definition. This guide walks through the practical steps to diagnose and resolve it.
What You'll Need
- Access to a PostgreSQL superuser account or a role with sufficient privileges to grant permissions and alter functions.
- The name of the function or procedure that raised the error, and the role that called it.
- A PostgreSQL client such as psql, pgAdmin, or any SQL interface.
- Basic familiarity with SQL GRANT and ALTER FUNCTION statements.
Step-by-Step
- Identify the exact function and calling role. The error message usually includes the function name and the role. If not, check the PostgreSQL server log for the full statement. You can also query
pg_stat_activityto see recent queries, but the log is more reliable. Note the function's schema and signature (argument types) because you will need them later. - Understand why the error occurs. Error 2F004 is raised when a function marked as
SECURITY DEFINERexecutes with the privileges of its owner, and that owner lacks theSELECTprivilege on a table or view the function tries to read. Alternatively, it can occur when a role has been explicitly deniedSELECTon a table viaREVOKE, and the function does not have the right to bypass that denial. The error is a security feature, not a bug. - Check the function's security mode. Run
\df+ schema.function_namein psql or querypg_procto see if the function isSECURITY DEFINERorSECURITY INVOKER. If it isSECURITY DEFINER, the owner's privileges matter. If it isSECURITY INVOKER, the caller's privileges matter. This determines which role needs the grant. - Grant SELECT on the underlying table(s). If the function is
SECURITY INVOKER, grantSELECTon the table(s) the function reads to the calling role:GRANT SELECT ON TABLE schema.table_name TO calling_role;. If the function isSECURITY DEFINER, grant the privilege to the function owner instead. UseGRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO role_name;if multiple tables are involved, but be cautious about over-granting. - Consider using a view or a security barrier. If you cannot grant direct table access, create a view that selects only the needed columns and grant
SELECTon the view. Then modify the function to read from the view. This limits exposure. Alternatively, useSECURITY DEFINERwith a carefully controlled owner that has only the necessary privileges. - Verify the grant took effect. Re-run the function call as the original role. If the error persists, check for role membership and inherited privileges. Use
\dp schema.table_nameto see current grants. Also check if the role has been explicitly denied viaREVOKE; a denial overrides grants. In that case, you must revoke the denial or use a different role. - If the function is in an extension or system catalog, do not alter it directly. Instead, grant the necessary privileges to the calling role or use a wrapper function with
SECURITY DEFINERowned by a privileged role. Never modify system functions. - Test and document. After making changes, test the function with the original role. Document the grant so future changes do not break it. Consider adding a comment to the function or a note in your migration scripts.
Common Mistakes
- Granting to the wrong role. For
SECURITY DEFINERfunctions, granting to the caller does nothing; you must grant to the owner. Check the function's security mode first. - Ignoring explicit denials. A
REVOKEon a table for a role overrides anyGRANTto a group the role belongs to. Use\dpto see all grants and denials. - Using
SECURITY DEFINERcarelessly. It runs with the owner's privileges, which can be a security risk if the function is not carefully written. Always set a safesearch_path. - Forgetting to grant on sequences or other objects. If the function also inserts or updates, you may need
USAGEon sequences orINSERT/UPDATEprivileges. - Altering system functions. Never modify functions in
pg_catalogor extension schemas; upgrade them via the extension mechanism instead.
Tips
- Use
\df+in psql to quickly see a function's owner and security mode. - Prefer
SECURITY INVOKERfunctions when possible; they are safer because they run with the caller's privileges. - When using
SECURITY DEFINER, always setsearch_pathto a trusted schema to prevent privilege escalation. - Regularly review grants with
\dpand\dn+to avoid accidental denials. - Test permission changes in a staging environment before applying to production.
Troubleshooting
If the error persists after granting SELECT, check the following:
- Role membership: The role may inherit privileges from a group that has been denied. Use
\duto see role memberships and\dpto see all grants. - Function owner: Confirm the owner with
\df+. If the owner is not the role you granted to, grant to the correct owner. - Schema usage: The role also needs
USAGEon the schema containing the table. Grant it withGRANT USAGE ON SCHEMA schema_name TO role;. - Row-level security: If row-level security is enabled, the function may need to be owned by a role that bypasses RLS, or you must add policies. Check
pg_policies. - Connection pooling: If using a connection pooler, ensure the session role is correct; some poolers reset roles.
- Logs: Check the PostgreSQL log for the exact statement and role; it often includes the function name and the missing privilege.
If you cannot grant the privilege due to security policies, consider creating a wrapper function with SECURITY DEFINER owned by a role that has the necessary rights, and grant EXECUTE on that wrapper to the calling role. Ensure the wrapper is safe and does not expose more data than intended.
Frequently Asked Questions
What does error 2F004 mean exactly?
It is the SQLSTATE for read_sql_data_not_permitted, raised when a role attempts to read SQL data without the required privilege, typically inside a function or procedure.
Can I fix it by granting SELECT to public?
Granting to PUBLIC is possible but risky; it gives every role access. Prefer granting to specific roles or using a secure view.
Why does the error appear only for some users?
Because privileges are role-specific. Users with different role memberships or explicit denials will see different behavior. Check each role's grants and denials.
Does this error occur in all PostgreSQL versions?
Yes, SQLSTATE 2F004 is part of the SQL standard and is used consistently across supported PostgreSQL versions. The exact context may vary with function security settings.
Can I disable this check?
No, it is a core security feature. You must adjust privileges or function definitions. Disabling it would require modifying PostgreSQL source, which is not recommended.
What if the function is owned by a superuser?
Superusers bypass privilege checks, so the error would not occur for a SECURITY DEFINER function owned by a superuser. If you see the error, the owner is not a superuser or the function is SECURITY INVOKER.