How to Connect Claude Code to a Database (Safely)

David IyaDavid Iya July 24, 2026 8 min read
A close-up of a server rack with network cables and blue indicator lights in a dim data aisle, representing a database Claude Code connects to
Original image, Claude Code Club

How to Connect Claude Code to a Database: The Short Answer

To connect Claude Code to a database, you give it a bridge rather than a password. That bridge is an MCP server - a small program that exposes your database to Claude as a set of safe actions like list tables, describe a table, and run a query. You point that server at a read-only connection, add it to Claude Code (the desktop app has an MCP settings panel, and the terminal has a command for the same thing), and from then on Claude can explore your data and answer questions about it without you ever pasting rows into the chat. The entire safety of this setup rests on one choice: the connection is read-only, so the worst Claude can do is read.

What an MCP Server Actually Does

MCP stands for Model Context Protocol. It is the standard way Claude Code talks to outside systems - databases, APIs, file stores. A database MCP server sits between Claude and your database and translates Claude's requests into real queries. Instead of Claude holding your credentials, the server holds them, and Claude only sees the actions the server chooses to offer: read the schema, list tables, run a SELECT. This is why the bridge is safer than direct access - you decide at the server what Claude is allowed to touch.

  • It connects to your database using a connection string you supply through an environment variable, not through the chat.
  • It exposes a small, fixed set of tools to Claude - typically list tables, describe a table's columns, and run a query.
  • It returns results to Claude as text, so Claude can reason about your real schema instead of guessing at table and column names.
  • It enforces whatever the database user is allowed to do - which is why the next step, a read-only user, is the one that matters most.

Step 1: Create a Read-Only Database User

Before you connect anything, make a dedicated database user that can read and nothing else. Do not point Claude at your application's main user or an admin account. Create a fresh user, grant it SELECT on the tables or schema you want Claude to see, and stop there. On Postgres that is a CREATE USER followed by a GRANT SELECT; most databases have the same two steps. The point is that even a badly phrased request - or a confused model - cannot delete, update, or escalate, because the account it is using was never given the power to.

Step 2: Add the Database MCP Server to Claude Code

With a read-only user in hand, add the database MCP server to Claude Code and give it the read-only connection string through an environment variable. In the desktop app you do this in the MCP settings; from the terminal there is an add command for the same thing. Put the connection string in your environment or a local secrets file that is git-ignored - never inline it in a prompt, and never commit it. When the server starts, Claude Code loads it as a new source of tools it can call during a session.

Where each piece of the bridge lives

PieceWhere it belongsWhy
The connection stringAn environment variable or git-ignored secrets fileIt is a credential. It must never sit in a prompt, a commit, or the chat history.
The database userYour database, granted SELECT onlyThe user, not Claude, is what actually enforces read-only.
The MCP serverClaude Code's MCP config (desktop panel or terminal add command)It holds the credential and exposes only safe actions to Claude.

Step 3: Verify the Bridge Before You Trust It

Do not assume the connection is safe because it works. Confirm it. In Claude Code, check that the database server is listed and connected, then ask Claude to list the tables and describe one - that proves the read path works. Then test the guardrail directly: ask it to make a trivial write, like inserting a throwaway row, and confirm the database refuses because the user has no permission. A bridge you have watched fail closed is one you can trust. A bridge you only watched succeed is one you are hoping about.

  1. Confirm the server shows as connected in Claude Code's MCP list.
  2. Ask Claude to list tables and describe one - the read path should just work.
  3. Ask it to attempt a write, and confirm the database rejects it for lack of permission.
  4. Only after you have seen it fail closed should you start relying on it for real questions.

The Read-Only Bridge: Why This Is the Only Safe Default

The Read-Only Bridge is the CCC name for this whole pattern: a least-privilege user, a connection string kept out of the chat, and an MCP server that exposes read actions only. It is the default because it makes the dangerous version impossible rather than merely discouraged. You are not trusting Claude to behave - you are removing its ability to misbehave. That is the same principle that keeps a production database safe from a junior engineer: not a promise to be careful, but permissions that make carelessness harmless.

When You Actually Need Write Access

Sometimes reading is not enough - you want Claude to help run a migration or seed test data. Do not solve this by loosening the read-only user. Create a second, separate user with exactly the writes that task needs, use it for that task only, and review every statement before it runs. Better still, have Claude generate the migration as a file you read and apply yourself, so the review step is built in. Write access is a deliberate, scoped exception you turn on for a job and turn off after, not a permanent property of the bridge.

Join the Club

This read-only-first approach to connecting Claude Code to real systems is exactly the kind of thing we build and pressure-test together inside Claude Code Club. If you want the working configs, the safety checklists, and other members who are wiring Claude into their own stacks, join the Club and grab the newsletter - that is where the details that do not fit in a blog post live.

Free Claude Code drops, straight to your inbox

Short, practical drops on skills, MCP, agents, prompts, and more. No spam, unsubscribe anytime.

Frequently asked questions

Do I need to know SQL to connect Claude Code to a database?

No. Once the read-only bridge is set up, you ask questions in plain English and Claude writes the SELECT queries for you against your real schema. Knowing a little SQL helps you sanity-check what it runs, but it is not required to get answers out of your data.

Which databases can Claude Code connect to?

Any database that has an MCP server - Postgres, MySQL, SQLite, and others all have them. The pattern is identical regardless: a read-only user, a connection string in an environment variable, and the MCP server as the bridge.

Is it safe to connect Claude Code to my production database?

It is safe to connect it to a read-only user on that database. Never connect it as an account that can write or administer. If you would not hand the credential to a stranger, do not put it behind Claude without stripping it down to read-only first.

Where do I put the database password?

In an environment variable or a git-ignored secrets file that the MCP server reads. Never paste it into a prompt and never commit it. The connection string is a credential, and the chat history is not a safe place for credentials.

Last reviewed by David Iya on July 24, 2026

David Iya

Written by

David Iya

Forbes 30 Under 30 · Y Combinator

Keep reading

Claude CodeGit

How to Use Claude Code with Git (Without Losing Work)

The safe way to use Claude Code with Git is to commit before you let it touch anything, work on a branch, and review the diff before you accept. Here is the checkpoint habit I use so an AI editing my files can never cost me work I cannot get back.

Duncan Rogoff 7 min
Read article

Ready to build it yourself?

Join Claude Code Club, the #1 community for learning claude code, for $9/month.

← Back to the blog