How to give your AI agent a Postgres database
Updated
Create a Kaiva Cloud database. Your app gets a connection string that reads and writes, and Claude, ChatGPT, Cursor and any other AI client get your tables as tools through a managed MCP server, with no server to set up. For a database you already have, connect it with Kaiva Bridge.
With Kaiva Cloud, in one step
Create a database in the Kaiva Cloud console. You get a connection string for your app, and each table's tools reach Claude, ChatGPT, Cursor and any other AI client through a managed MCP server: at once when you choose Check for tables, or within about 15 minutes while the database is in use. AI clients read through a read-only login, and writes stay off unless you switch them on. Your app keeps full access.
The steps below are the same setup by hand, for a database you run somewhere else.
Step by step
- Create the databaseAny Postgres works: one on your machine, or one at a hosted provider. Keep its connection string in your project's .env, never in a chat with the AI.
- Create a login for the agentMake a login that can only read the tables the agent needs. The guide to a read-only Postgres login has the SQL.
- Give that login to an MCP serverOn your machine, add Kaiva Postgres MCP to Claude Code, Cursor or Claude Desktop with the agent's login. For Claude.ai, ChatGPT or a team, paste the login's connection string into Kaiva Bridge, which serves the tables as a managed MCP server.
- Ask it about your dataAsk a question only your data can answer, such as how many orders came in this week. The agent picks a tool, the server runs it as the read-only login, and the answer comes from real rows.
In Claude Code, with the agent's login in the URL:
claude mcp add postgres -- npx -y @kaiva/postgres-mcp "postgresql://ai_reader:PASSWORD@HOST:5432/shop"Why two logins
Your app needs to write. An agent answering questions needs to read. With one shared login, anything the agent sends runs with your app's rights, delete included. With its own login, PostgreSQL itself refuses a write, whatever the agent asks.
Why an MCP server, not the connection string
Claude, ChatGPT, Cursor and other AI clients reach databases through MCP servers. The server holds the password and gives the agent tools, so the model never sees the password. ChatGPT and Claude.ai can only use a remote server; Claude Code, Cursor and Claude Desktop can also start one on your machine.
Agents you build yourself
If you build the agent with an SDK or an agent framework, connect it to the same MCP server, or give your code the read-only connection string and let it run its own queries.
Kaiva Postgres MCP is free and open source. Kaiva Bridge's Free plan includes 10 MCP servers and 200,000 tool calls a month, with no card.
Other ways to do it
- A database the agent creates itselfSome hosts let an agent create a Postgres database with one command and no account, and you claim it to keep it; Prisma Postgres and Neon do. Good for trying an idea. Give the agent a read-only login before real data goes in.
- Your app's connection stringQuick, and the agent can then do anything your app can, including delete. Use it only on a database you can throw away.
- Your host's own MCP serverSupabase, Neon and others run MCP servers that manage the whole project. Good for building with an agent; more access than an agent needs to answer questions.
Questions
- Can the agent write to the database?
- Not with a read-only login: PostgreSQL refuses. If an agent must write, give it narrow tools that each do one thing, not a login that can write anywhere.
- Where should the connection string live?
- In .env for your app, and in the MCP server's configuration for the agent. Never in a prompt or a chat.
- Does this work with ChatGPT?
- Yes, through a remote MCP server such as Kaiva Bridge. ChatGPT can't start a server on your machine.
- Is it free?
- Kaiva Postgres MCP is free and open source. Kaiva Bridge's Free plan includes 10 MCP servers and 200,000 tool calls a month, with no card.
- Where is a Kaiva Cloud database stored?
- In the EU, with its files.