How to connect Claude Code to a Postgres database
Updated
Run claude mcp add with Kaiva Postgres MCP and your database's connection URL. It is a free, open-source MCP server that runs on your machine and lets Claude Code list your tables, read their columns and run read-only queries. No account is needed.
Step by step
- Add the serverIn your project folder, run the first command below with your own connection URL. Claude Code starts the server when it needs it. Node.js 18 or later must be installed.
- Check it worksRun claude mcp list: postgres should show as connected. Then ask Claude Code which tables the database has.
- Use a login that can only read (recommended)PostgreSQL then refuses any write, whatever is sent. The guide to a read-only Postgres login, linked at the end of this page, has the SQL. To see what a login can do, run the command with --check: it connects once, reports whether the login is a superuser and whether it can change data, and exits.
In a terminal:
claude mcp add postgres -- npx -y @kaiva/postgres-mcp postgresql://localhost/mydb
claude mcp list
npx -y @kaiva/postgres-mcp postgresql://localhost/mydb --checkIn every project
claude mcp add saves the server for the current project folder only. To have it in every project, put --scope user before the name: claude mcp add --scope user postgres -- npx -y @kaiva/postgres-mcp, followed by your connection URL.
The four tools
query runs one read-only SQL statement (SELECT, WITH, VALUES, TABLE, SHOW or EXPLAIN) and returns the rows as JSON. list_tables lists the tables and views the login can read. describe_table shows one table's columns, keys and indexes. database_health shows the largest tables and indexes, indexes that are never used, the slowest queries (with their values hidden) and the cache hit rate.
Other ways to do it
- Kaiva Bridge, for Claude.ai, ChatGPT or a teamWant this database in Claude.ai, ChatGPT or any other AI client, or for your team? If it's on Neon, Supabase or any other address on the internet, Kaiva Bridge serves it with one command, run where DATABASE_URL is set: npx @kaiva/cli bridge push mydb --db.
Questions
- Is it free?
- Yes. Kaiva Postgres MCP is open source under the MIT licence, and needs no account.
- Can the AI change my data?
- Not through this server: one read-only statement per call, in a transaction that is always rolled back. A view or function already in your database runs with the login's rights, so use a login that can only read.