Plugins & MCP Beginner 9 min read

MCP for Databases: Query Postgres with Claude

Learn how to connect Claude directly to your Postgres database using MCP servers for natural language database queries.

When I first heard about connecting Claude to a database, I thought "this sounds way too complicated for someone like me." I mean, I was still getting comfortable with basic SQL queries, and now I'm supposed to set up some fancy connection between an AI and my database?

Turns out, it's actually pretty straightforward once you know the steps. And honestly? It's been a game-changer for exploring data. Instead of remembering complex SQL syntax, I can just ask Claude "show me all users who signed up last month" and get the results instantly.

Let me walk you through exactly how to set this up with PostgreSQL.

What You'll Need Before Starting

Before we dive in, make sure you have these basics covered:

  • Claude Desktop app installed
  • A PostgreSQL database running (locally or remote)
  • Node.js installed on your machine
  • Basic familiarity with command line

Don't worry if your database is pretty simple right now. I started testing this with a basic table of users, and it worked perfectly. You don't need some complex enterprise setup.

Installing the Postgres MCP Server

The easiest way to get started is with the official PostgreSQL MCP server. This is maintained by Anthropic, so you know it's going to work well with Claude.

terminal
# Install the PostgreSQL MCP server globally
npm install -g @modelcontextprotocol/server-postgres
→ Installing MCP server...

Once that's done installing, you'll need to configure Claude Desktop to use this server. This is where we tell Claude how to connect to your specific database.

Configuring Claude Desktop

Now we need to add the MCP server configuration to Claude Desktop. You'll need to edit your Claude configuration file.

On Mac, this file is located at:

~/Library/Application Support/Claude/claude_desktop_config.json

On Windows, look for:

%APPDATA%/Claude/claude_desktop_config.json

Here's what you need to add to that file:

claude_desktop_config.json
{
"mcpServers": {
"postgres": {
"command": "mcp-server-postgres",
"args": [
"postgresql://username:password@localhost:5432/database_name"
]
}
}
}

Replace the connection string with your actual database details. If you're using a local PostgreSQL installation, it might look something like:

postgresql://myuser:mypassword@localhost:5432/myapp_development

Security Note

Never put production database credentials in config files like this. Use environment variables or a dedicated development database for testing.

Testing the Connection

After saving your config file, restart Claude Desktop completely. When you open it back up, you should see a small indicator that MCP servers are connected.

Let's test it out with a simple query. I like to start with something basic to make sure everything's working:

claude prompt
Can you show me what tables are available in my database?

If everything is set up correctly, Claude should be able to connect to your database and list your tables. When I first got this working, seeing Claude actually return my table names felt like magic.

Practical Database Queries with Claude

Once you've confirmed the connection works, you can start asking Claude to help with actual database tasks. Here are some examples that I use regularly:

example queries
# Data exploration
"Show me the structure of the users table"

# Simple queries
"How many users signed up this month?"

# More complex analysis
"What are the top 5 most popular product categories by sales?"

# Data validation
"Are there any users with duplicate email addresses?"

What I love about this setup is that Claude doesn't just run the queries—it explains what it's doing and often suggests follow-up questions. It's like having a database analyst sitting next to you.

Working with Real Data

Let me share a real example from when I was analyzing user signup data. I had a table called users and wanted to understand signup patterns.

claude conversation
Me: "Can you show me user signups by month for the last 6 months?"

Claude: I'll query your users table to show signups by month.

SELECT
DATE_TRUNC('month', created_at) as signup_month,
COUNT(*) as user_count
FROM users
WHERE created_at >= CURRENT_DATE - INTERVAL '6 months'
GROUP BY DATE_TRUNC('month', created_at)
ORDER BY signup_month;

Not only did Claude write the perfect SQL query, but it also formatted the results nicely and suggested that I might want to look at signup sources or user retention next. This kind of guided analysis has helped me discover insights I wouldn't have thought to look for.

Common Issues and Solutions

When I was first setting this up, I ran into a few hiccups that are worth mentioning:

Connection refused errors: Make sure your PostgreSQL server is actually running and accepting connections. I spent 20 minutes troubleshooting before realizing I'd forgotten to start my local Postgres service.

Authentication failures: Double-check your username and password in the connection string. Also make sure your database user has the necessary permissions to read the tables you want to query.

MCP server not loading: If Claude Desktop doesn't show the MCP connection, try completely quitting and restarting the app. Sometimes it needs a full restart to pick up config changes.

Testing Tip

Start with a simple read-only database or create a test database with sample data. Don't experiment on production data while you're learning.

What This Opens Up

Having Claude connected to your database changes how you work with data. Instead of remembering SQL syntax or writing complex queries from scratch, you can focus on asking the right questions.

I've used this setup for:

  • Quick data validation during development
  • Exploring datasets I'm unfamiliar with
  • Generating reports without writing SQL
  • Learning better SQL practices (Claude explains what each query does)

The best part? You're not just getting query results—you're getting explanations, suggestions, and follow-up questions that help you think about your data in new ways.

Once you've got this working with PostgreSQL, you can explore MCP servers for other databases too. There are servers for MySQL, SQLite, and even non-relational databases. The pattern is the same: install the server, configure Claude, and start querying with natural language.

Want to go deeper?

Check out more tutorials in this category, or explore the full site.