How to Fix Postgres Error 38001 Containing SQL Not Permitted

Published
Length
3 min read

Luis Felipe Dias

All posts

You see postgres error 38001 containing sql not permitted when an external routine runs SQL. You fix it by removing SQL queries from that routine or changing its settings. Database engines use strict rules to keep external code safe during a query run. When a routine breaks these rules, the database stops the task right away. This guide explains why this happens and how your team can fix it. What causes postgres error 38001 containing sql not permitted In PostgreSQL, state 38001 maps to the

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You see postgres error 38001 containing sql not permitted when an external routine runs SQL. You fix it by removing SQL queries from that routine or changing its settings.

Database engines use strict rules to keep external code safe during a query run. When a routine breaks these rules, the database stops the task right away. This guide explains why this happens and how your team can fix it.

What causes postgres error 38001 containing sql not permitted

In PostgreSQL, state 38001 maps to the error spec for containing SQL not permitted. The engine tracks this fault with the internal macro ERRCODE_E_R_E_CONTAINING_SQL_NOT_PERMITTED. It belongs to a class of errors for external routine exceptions.

Databases let teams write custom routines in outside languages like C or Python. When you set up an external routine, you tell the engine what it does. Some routines only do math or format text without touching any database tables. If a routine is marked as having no SQL, the engine enforces that rule. If the code then tries to run a query, the engine throws error 38001.

This check protects the database from hidden data changes and unexpected locking issues. Foreign data wrappers and custom extensions can also trigger this exact state. When an external call tries to run commands against the host database, it fails. The database stops the whole transaction to keep your data safe and clean.

How to diagnose and fix the failure in production

To find the root cause, check your server logs right when the error happens. The log entry will show which function or extension ran the blocked command. Look at the routine definition to see how it interacts with the database.

You have two safe ways to fix this fault in a live system. First, you can move the SQL logic out of the external code. Let your main app or a standard database procedure run the query instead. Then pass the plain values into the external function for math or text work. This keeps your external code simple, fast, and easy to test.

End-to-End Encryption + SQL Query Filtering: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Second, if the routine must run queries, update its definition to allow SQL. Make sure the routine only gets the exact database rights it needs to work. Never fix routine errors by giving the caller broad superuser rights.

Managing routine rights and table access

When you fix external routines, you should also review who can run them. In PostgreSQL, the special role name PUBLIC grants rights to every role on the system. By default, PUBLIC gets EXECUTE rights on new functions and procedures. It also gets CONNECT and TEMPORARY rights on databases. The object owner can revoke these default rights to lock down the system.

For better control, set up group roles to manage rights for many users. You can grant specific rights to the group role and add users to it. If a routine reads data, scope its table rights down to the smallest level. The SELECT right lets a role read specific columns of a table or view. That same right also allows COPY TO operations on the object.

You should also check schema creation rules on older database setups. In PostgreSQL 15 and later, the default setup blocks users from creating public objects. On older versions or upgraded databases, an admin must revoke CREATE on public from PUBLIC. Pair tight role rights with data masking that rewrites emails and card numbers in results. This stops bad routines from leaking private fields like phone numbers or national IDs.

Run guardrails and masking on the Postgres connection

Role grants and firewall rules are needed setup. Setup alone cannot check single SQL statements or mask sensitive rows in a response. By design, hoop.dev sits inline in the Postgres protocol beside Postgres, RDS, or Aurora as a sidecar for your Postgres. On every production query from an engineer or AI agent, hoop.dev decides whether to pass, mask, block, or hold it for approval.

First, hoop.dev guardrails refuse disallowed drops or deletes before they run, and risky statements can pause for human review. Next, hoop.dev data masking rewrites sensitive fields in returned rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and logs every statement and verdict per resource. Check out the project on GitHub.

Frequently asked questions

What does SQLSTATE 38001 mean in PostgreSQL?

SQLSTATE 38001 is an external routine exception in PostgreSQL. It means an external function tried to run SQL when its setup forbids SQL commands.

Can granting superuser rights fix error 38001?

No, role rights do not change how the engine checks external routine rules. You must either remove the SQL call from the routine or update its definition.

How do I stop all users from running a new PostgreSQL function?

By default, the PUBLIC role gets execute rights on new functions and procedures. The function owner must revoke execute rights from PUBLIC and grant them to specific group roles.

The query runs. The emails stay hidden.