Securing LangChain access to Google Cloud SQL Postgres
Connect a LangChain agent to Cloud SQL for PostgreSQL by pointing its database connection at a proxy that checks every statement. Use IAM database authentication plus pgAudit to manage identity and logging. This setup keeps the agent's access scoped. It adds a runtime check that stops dangerous SQL before it executes. Consider a CI job that runs a LangChain workflow. It generates reports from a production database. The job uses a service account with IAM database authentication. It logs in with
Customer Support Access to Production + SQL Query Filtering: the complete guide
Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.
Connect a LangChain agent to Cloud SQL for PostgreSQL by pointing its database connection at a proxy that checks every statement. Use IAM database authentication plus pgAudit to manage identity and logging. This setup keeps the agent's access scoped. It adds a runtime check that stops dangerous SQL before it executes.
Consider a CI job that runs a LangChain workflow. It generates reports from a production database. The job uses a service account with IAM database authentication. It logs in with a short-lived token. It does not use a static password. The agent has SELECT rights on the sales schema. The LLM decides to run a query. It joins the sales table with the customer table. That table contains email addresses. The agent returns those emails in its response. No mechanism masks them before the data leaves the database. pgAudit logs the statement. But the sensitive values have already reached the client.
How LangChain connects to Cloud SQL Postgres
LangChain agents typically use a database toolkit. They use it to execute SQL queries. The agent generates a query based on the user's prompt. It also uses the schema information it has read. The query runs with the permissions of the connected credential. If the credential has INSERT or UPDATE rights, the agent can modify data. The framework does not inspect the query for destructive intent. It executes what the model generates.
Cloud SQL for PostgreSQL provides IAM database authentication. It lets a service account log in with a short-lived OAuth 2.0 access token. It does not use a password. This reduces the risk of standing credentials leaking. You must grant the service account specific database privileges. For example, grant SELECT on the tables the agent needs. Cloud SQL does not have a built-in dynamic data masking feature. It supports the postgresql_anonymizer extension.
What IAM authentication and pgAudit do and do not cover
IAM database authentication solves the identity problem. It uses short-lived tokens that expire after one hour. pgAudit records the SQL statements that execute on the instance. You can enable it by setting a flag. You also create the extension. The logs are sent to Cloud Logging. Together, these controls tell you who connected. They also tell you what they ran. They do not tell you whether the statement was safe. They do not modify the data that comes back to the client.
The gap is runtime control. A read-only role stops writes. It does not stop an agent from reading sensitive data. The agent should not see that data. You need a check that runs on every statement. It must run in real time. It must run on the connection itself.
Adding statement-level checks with hoop.dev
hoop.dev is an open-source proxy. It runs next to your Cloud SQL instance. It reads every statement the agent sends. It also reads every result that comes back. It speaks the PostgreSQL protocol. It understands the structure of each query. It can refuse a destructive statement. For example, it can stop a DELETE with no WHERE clause. It stops it before it reaches the database. It can also rewrite sensitive values in the result set. For example, it can hide email addresses. It does this before they reach the client. The agent does not know the proxy is there. You only change the host and port in its connection string.
Customer Support Access to Production + SQL Query Filtering: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
The guardrails feature lets you define rules. These rules match the effect of a statement. They also match the table it touches. They can match patterns in the query. If a rule matches, hoop.dev refuses the statement. It returns a PostgreSQL error with a message you write. The agent can read why it was refused. The data masking feature rewrites sensitive values in the response. It does this in memory. It does this before they reach the client. It uses an open-source PII detector. It identifies emails, phone numbers, and other sensitive data types. You can configure rules to mask specific columns. You can also mask detected types. The masking happens on the connection. It does not happen in the database engine.
For a LangChain agent, this means the agent can still generate queries. But the proxy decides what runs. The proxy also decides what comes back. If the agent tries to read a column with email addresses, the proxy masks them. If the agent tries to run a DROP TABLE statement, the proxy refuses it. The audit trail records every statement. It also records every decision.
Setting up the proxy
To set up hoop.dev for your Cloud SQL instance, run the proxy next to the database. You can run it in the same pod. You can also run it on the same host. You configure it with a single config file. The agent's connection string points to the proxy. It does not point to the database host. Existing credentials and tools stay the same.
You can find the setup instructions in the PostgreSQL protocol documentation. The docs cover how to configure the proxy. They also cover how to define rules. They show how to connect your client. For a quick start, see the quickstart guide. To explore the source code, visit the GitHub repository. You can also run the proxy locally there.
When to use each control
Use IAM database authentication to manage identity. It reduces the risk of standing credentials. Use pgAudit to log statements for compliance. You can also use it for debugging. Use hoop.dev when you need a runtime check. It applies to every statement. It runs on the connection itself. These controls stack. They do not replace each other. IAM auth decides who connects. pgAudit logs what they do. hoop.dev checks what each statement does. It also checks what comes back.
Frequently asked questions
Does hoop.dev replace IAM database authentication? No. IAM authentication manages identity and access to the database. hoop.dev manages what each statement does. It also manages what comes back. You use both together. IAM auth decides who can connect. hoop.dev checks every statement on the connection.
Can hoop.dev mask data that the agent should not see? Yes. Data masking rewrites sensitive values in the result set. It does this before they reach the client. You can configure rules to mask specific columns. You can also mask detected types. Examples include emails or phone numbers. The masking happens in memory. It happens on the connection. The database stores the original data.
Does hoop.dev work with LangChain agents? Yes. hoop.dev speaks the PostgreSQL protocol. It works with any client that uses that protocol. This includes LangChain. The agent only changes the host and port in its connection string. You do not need an SDK. You do not need prompt changes. You do not need agent-side configuration.
In this post