← Back to Blog

Tutorial: Connect PostgreSQL to Claude via MCP

Vibe Manager Team

Connecting an LLM to your database feels like magic. You can ask “Who are the top 5 users by spend?” and it writes the SQL, executes it, and graphs the result.

Here is how to set up the PostgreSQL MCP Server.

Prerequisites

  • A running Postgres database (Local or Remote).
  • Connection string (e.g., postgresql://user:pass@localhost:5432/mydb).

Step 1: Install the Server

We will use the official implementation. npx -y @modelcontextprotocol/server-postgres <connection-string>

Step 2: Configure Claude

Add this to your claude_desktop_config.json:

"postgres": {
  "command": "npx",
  "args": [
    "-y",
    "@modelcontextprotocol/server-postgres",
    "postgresql://user:password@localhost:5432/my_database"
  ]
}

Step 3: The First Interaction

Restart Claude. Now, you need to “prime” it. The AI doesn’t know your schema yet.

Prompt:

“Examine the database schema. List all tables.”

Claude will call the list_tables tool. It will see users, orders, products.

Step 4: Complex Queries

Now you can ask business questions.

Prompt:

“Calculate the average order value for users who signed up in 2024.”

Claude will:

  1. Look at the users and orders table definitions (columns).
  2. Construct a SQL query (JOIN, WHERE, AVG).
  3. Run the query via the query tool.
  4. Tell you the answer: “The AOV is $142.50.”

Safety Note

The official server allows raw SQL execution.

  • Warning: If you ask Claude to “Delete all bad users,” it can execute DELETE FROM users.
  • Mitigation: Use a database user that only has SELECT permissions if you want to be safe.

Why use Vibe Manager?

Managing database credentials in cleartext JSON files is risky. Vibe Manager helps you manage these configs and can help keep your connection strings organized across different projects.

Manage Configs

Sync your Claude, Cursor, and Codex configurations in one click with Vibe Manager.

Download Vibe Manager

Find Verified Skills

Discover and install secure, community-verified MCP skills and agent rules from SkillMap.

Browse SkillMap ↗

Related Articles