How to Configure OpenHands Postgres Least Privilege
You set up openhands postgres least privilege by creating a dedicated database role. You then grant that role read permissions on specific tables or columns only. Teams often give an AI agent a broad database account to save time. That broad account lets the agent alter schemas, drop tables, or read private rows. A tight role setup stops the agent from touching unauthorized database objects. How openhands postgres least privilege works in the database PostgreSQL manages permissions through ro
Least Privilege Principle + End-to-End Encryption: the complete guide
Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.
You set up openhands postgres least privilege by creating a dedicated database role. You then grant that role read permissions on specific tables or columns only. Teams often give an AI agent a broad database account to save time. That broad account lets the agent alter schemas, drop tables, or read private rows. A tight role setup stops the agent from touching unauthorized database objects.
How openhands postgres least privilege works in the database
PostgreSQL manages permissions through roles that act as users or groups. You start by setting up a group role for your automated agents. You then create a specific login role for the agent and add it to the group. This setup helps your team manage permissions across many users in one place.
Next, you must check the built-in PUBLIC role on your database server. The PUBLIC role gives rights to every role on the system by default. Every new role gets database login rights, temporary table rights, and function execution rights. An object owner must revoke these default PUBLIC grants to lock down the database.
You also need to check permissions on the default public schema. In PostgreSQL 15 and newer, users cannot create objects in the public schema by default. Older databases, or upgraded systems, still allow object creation there unless you revoke it. Once you remove those defaults, you grant the agent role usage on target schemas.
Finally, you grant the SELECT privilege on the exact tables the agent needs. PostgreSQL lets you grant SELECT on a whole table or on specific columns only. Column grants let the agent read safe metadata while blocking sensitive columns. Keep in mind that the SELECT privilege also allows bulk data export operations.
What native database roles solve and what they miss
Native database roles decide which user can log in and which objects it can touch. If the agent tries to drop a table, the database blocks the command right away. If the agent queries a restricted column, PostgreSQL rejects the query before returning data. This native layer is essential for basic database safety.
Least Privilege Principle + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Still, static database grants cannot inspect what a valid query does in flight. A role with table read rights can still pull thousands of customer emails at once. Native column blocks are also all-or-nothing and often break app queries that select entire rows. When you need to mask phone numbers or card numbers inside results, static roles fall short.
Why agent workflows need statement-level controls
AI coding agents generate and run SQL queries on their own during debugging tasks. You cannot predict every query the agent will write before a session starts. Static credentials stay active in the agent workspace long after the task finishes. If a prompt causes the agent to dump a table, database roles allow it.
Teams also struggle to audit which human prompt triggered a specific database query. Database logs only show the shared role name that ran the statement. To control live queries and rewrite sensitive fields like national IDs, you need runtime controls.
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 is an open-source proxy that sits beside Postgres, RDS, or Aurora as a sidecar for your Postgres. Clients and agent tools keep their credentials and point their connection string at the listener.
On each query, hoop.dev guardrails refuse disallowed drops, deletes, or table reads before they run. Next, hoop.dev data masking rewrites sensitive fields in returned rows that match your rules. 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
Can I grant an OpenHands agent read access to specific PostgreSQL columns only?
Yes, PostgreSQL lets you grant the SELECT privilege on specific columns of a table or view. The database will reject any agent query that tries to read ungranted columns on that table.
Why can a new PostgreSQL role still run functions without explicit grants?
PostgreSQL grants the built-in PUBLIC role execution rights on functions and procedures by default. Every new role inherits PUBLIC rights unless the object owner revokes them.
Does the PostgreSQL SELECT privilege allow more than simple read queries?
Yes, the SELECT privilege also allows bulk export operations from the granted table or columns. You must account for this behavior when giving an automated agent read permissions.
Related guides
In this post