Claude is smart. PostgreSQL is sturdy. MCP is the friendly bridge that lets them talk. Put them together, and Claude can explore your database like a curious intern with a perfect memory and very polite manners.
TLDR: MCP means Model Context Protocol. It lets Claude connect to tools, files, APIs, and databases like PostgreSQL. You install a PostgreSQL MCP server, add it to Claude’s config, restart Claude, and then ask questions about your data. Use a read-only database user unless you really enjoy danger.
What Is MCP?
MCP is a standard way for Claude to connect to outside systems. Think of it as a plug socket. Claude is the lamp. PostgreSQL is the power source. MCP is the socket that helps everything connect safely.
Without MCP, Claude only knows what you paste into the chat. With MCP, Claude can call tools. It can ask a database for tables. It can inspect schemas. It can run queries if the server allows it.
This is useful when you want Claude to help with:
- Understanding database structure.
- Writing SQL queries.
- Debugging slow reports.
- Finding weird data patterns.
- Explaining tables to non-technical teammates.
What You Need
Before we start, gather your tiny toolbox.
- Claude Desktop with MCP support.
- Node.js installed on your machine.
- PostgreSQL running locally or on a server.
- A database connection string.
- A safe database user.
The safe user is important. Do not give Claude your superuser account. That is like giving a raccoon the keys to a bakery. It may be cute. It may also eat the walls.
Create a Read-Only PostgreSQL User
Start inside PostgreSQL. You can use psql, pgAdmin, or your favorite SQL tool.
CREATE USER claude_reader WITH PASSWORD 'strong_password_here';
GRANT CONNECT ON DATABASE your_database TO claude_reader;
GRANT USAGE ON SCHEMA public TO claude_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO claude_reader;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO claude_reader;
This gives Claude the power to read. Not write. Not delete. Not drop tables into a volcano.
If you use several schemas, repeat the schema grants for each one. For example, you may also need analytics, sales, or app.
Build Your Connection String
A PostgreSQL connection string looks like this:
postgresql://claude_reader:strong_password_here@localhost:5432/your_database
Let’s break it down.
- claude_reader is the username.
- strong_password_here is the password.
- localhost is the database host.
- 5432 is the usual PostgreSQL port.
- your_database is the database name.
If your password contains special characters, encode them. For example, @ becomes %40. This prevents the connection string from getting confused.
Install the PostgreSQL MCP Server
The common MCP server package for PostgreSQL is published through npm. You can run it with npx. That means you do not need a big install step.
Test it in your terminal first:
npx -y @modelcontextprotocol/server-postgres \
postgresql://claude_reader:strong_password_here@localhost:5432/your_database
If it starts without yelling, you are on the right path. Computers yell in errors. Silence is often good.
Add It to Claude Desktop
Now open the Claude Desktop configuration file. The exact location depends on your operating system.
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
Add an MCP server entry like this:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://claude_reader:strong_password_here@localhost:5432/your_database"
]
}
}
}
If you already have other MCP servers, do not erase them. Add postgres inside the existing mcpServers object. JSON is picky. Commas matter. Braces matter. Tiny punctuation goblins matter.
Save the file. Then fully quit Claude Desktop. Open it again.
Ask Claude to Use PostgreSQL
Now comes the fun part. Open Claude and ask a simple question.
- “What tables are in my PostgreSQL database?”
- “Describe the users table.”
- “Which tables seem related to orders?”
- “Write a query to show revenue by month.”
Claude may ask permission before using the tool. Say yes if the request looks correct. You are still the boss. Claude is the helpful owl with SQL glasses.
Good Prompts to Try
Good prompts make better results. Be clear. Give a goal. Ask for explanations.
- Data map: “Inspect the schema and summarize the main business objects.”
- Query help: “Write a SQL query for active customers who ordered in the last 30 days.”
- Debugging: “This query is slow. Explain why and suggest indexes.”
- Data quality: “Find columns that may contain missing or inconsistent data.”
- Plain English: “Explain the payments table to a product manager.”
Ask Claude to show the SQL before running it. This is a great habit. It keeps surprises away. Surprises belong at birthday parties, not in production databases.
Security Tips
Security is not the boring part. It is the seatbelt. It is also the reason you still have a database tomorrow.
- Use a read-only user by default.
- Connect to a development copy when possible.
- Do not expose private customer data unless approved.
- Store passwords safely.
- Review every query before it runs.
- Limit network access to the database.
If Claude needs write access, create a separate user. Grant only the exact permissions needed. Do not hand it the master key. A careful setup beats a dramatic cleanup.
Troubleshooting
If the integration does not work, do not panic. Most issues are small.
- Claude does not show the server: Restart Claude Desktop fully.
- JSON error: Check commas, quotes, and braces.
- Command not found: Install Node.js and make sure
npxworks. - Connection refused: Check the host, port, and PostgreSQL status.
- Authentication failed: Check username, password, and encoding.
- Permission denied: Add the needed
SELECTgrants.
Also check Claude’s logs if your app provides them. Logs are like breadcrumbs. Except they lead you out of the forest of confusion.
When to Use a Custom MCP Server
The basic PostgreSQL MCP server is great for many cases. Still, teams sometimes need more control.
You may want a custom MCP server if you need:
- Approved query templates only.
- Strict row limits.
- Audit logs for every request.
- Business-friendly tools like
get_monthly_revenue. - Special handling for private data.
A custom server can wrap database access in safer tools. Claude asks for a business action. Your server decides what SQL is allowed. This is cleaner for serious production work.
Final Thoughts
Claude MCP PostgreSQL integration is powerful. It turns your database into a conversation partner. You can ask questions, explore schemas, and create SQL faster.
Start small. Use read-only access. Test with harmless questions. Then build better workflows as you gain trust.
With MCP, Claude is not just chatting. It is helping you inspect real systems. And PostgreSQL is not just sitting there full of rows. It is finally telling its story.