How to connect PostgreSQL to your AI agent
Short answer: create a read-only PostgreSQL role, add the database to swarmvia as a source with that role, and add https://swarmvia.com/mcp to Claude, Cursor, or Codex. The agent gets source_schema and source_query tools for read-only SQL, and the password never leaves swarmvia. Works with Neon, Supabase, RDS, Aurora, and Redshift.
You want Claude, Cursor, or Codex to answer questions from your PostgreSQL
data. You do not want a postgres:// URL with a password sitting in an MCP
config file on every laptop.
This guide adds PostgreSQL to swarmvia as a source and connects your agent to it. The agent gets the schema and read-only queries. The password never leaves swarmvia. For why that matters beyond databases, see How to stop sharing credentials with AI agents.
It works with anything that speaks the PostgreSQL wire protocol: PostgreSQL, Neon, Supabase, Amazon RDS and Aurora PostgreSQL, Redshift, Timescale, CockroachDB, Materialize, and Greenplum.
Create a read-only role
Create a role with exactly the access you want the agent to have:
CREATE ROLE swarmvia_ro LOGIN PASSWORD '…';
GRANT CONNECT ON DATABASE analytics TO swarmvia_ro;
GRANT USAGE ON SCHEMA public TO swarmvia_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO swarmvia_ro;
ALTER ROLE swarmvia_ro SET default_transaction_read_only = on;
ALTER ROLE swarmvia_ro SET statement_timeout = '30s';GRANT USAGE ON SCHEMA is the line people forget. Without it the role logs in
and sees nothing, and every query reports that the relation does not exist.
The last two lines are defence in depth. swarmvia sets a read-only transaction and a statement timeout on every query anyway, but a role that is read-only in the database stays read-only regardless.
To expose only some tables, grant SELECT on those tables instead of the whole
schema. A view over the columns you are comfortable sharing is often the
cleanest boundary.
Use scram-sha-256
The role's password must be stored as scram-sha-256. md5 and cleartext are
refused. If your server still defaults to md5:
SET password_encryption = 'scram-sha-256';
ALTER ROLE swarmvia_ro PASSWORD '…';Create the source
- Go to Sources → New source and choose Postgres database.
- Enter the public hostname, port, database, and the
swarmvia_rocredentials. The host must be a hostname, not an IP address, because TLS verifies the certificate against it. - Leave TLS on Verify certificate. Neon, Supabase, and RDS all present certificates that verify. Use Require SSL only when a private CA is in the way.
- Click Test connection. It reports the role it authenticated as, whether the read-only guard took effect, and how many tables the role can see.
The database must be reachable from the public internet. For a private RDS instance, use a public read replica or a bastion host.
Connect the agent
Add one remote MCP server. Claude Code:
claude mcp add --transport http swarmvia https://swarmvia.com/mcpCursor (.cursor/mcp.json) or Claude.ai custom connectors take the same URL.
Approve the grant in the browser and pick the PostgreSQL source. See
Connect your agent for every client.
The agent now has:
| Tool | What it does |
|---|---|
source_schema |
Schemas, tables, and columns the role can see |
source_count_rows |
Size a table before querying it |
source_query |
Run up to 5 read-only statements with bound $1 parameters |
get_query_result |
Read the result of a long query that finished later |
Results come back as TSV in tool output, which costs fewer tokens than JSON for the same rows.
Long analytical queries
MCP clients have their own tool-call timeout. For a query that might run for
minutes, the agent sends waitMs: 0 and gets a job handle back immediately.
The same query keeps running, and the agent reads the result with
get_query_result when it is ready. Turn on Async under the source's
Concurrency settings to allow this.
Why not just give the agent a connection string?
- One credential, rotated in one place. Nobody else holds it.
- Per-person attribution. Logs show each statement and the key that ran
it. A permission-denied attempt (
42501) or read-only violation (25006) is recorded with the full statement. - A concurrency limit. The read replica sees at most as many queries as you allow, however many agents are running.
- Offboarding in one click. Disconnect the person's agent under Settings → Connected agents.
For a whole analytics team, see Give a data team Claude access to Postgres.