DEV Community

Cover image for Integrating Locally running Postgres with Claude Desktop
Ajeet Singh Raina
Ajeet Singh Raina

Posted on

6 1

Integrating Locally running Postgres with Claude Desktop

Just as USB-C provides a standardized way to connect your devices to various peripherals and accessories, MCP provides a standardized way to connect AI models to different data sources and tools.

MCP is an open protocol that standardizes how applications provide context to LLMs. MCP helps you build agents and complex workflows on top of LLMs. LLMs frequently need to integrate with data and tools, and MCP provides:

Image33

  • A growing list of pre-built integrations that your LLM can directly plug into
  • The flexibility to switch between LLM providers and vendors
  • Best practices for securing your data within your infrastructure

This guide walks you through setting up PostgreSQL locally on a MacBook, configuring a database, and integrating it with Claude Desktop for efficient data querying and management. Follow along to get started!

Refer this GitHub Repository for MCP Postgres

Getting Started

Prereq

  • MacBook
  • Install Claude Desktop
  • Install Postgres

Setting up Postgres locally

Install Postgres locally

To install PostgreSQL on your MacBook, use the following Homebrew command:

brew install postgresql
Enter fullscreen mode Exit fullscreen mode

Start Postgres Service

After installation, start the PostgreSQL service:

brew services start postgresql
Enter fullscreen mode Exit fullscreen mode

Result:

 brew services start postgresql
==> Successfully started `postgresql@14` (lab
Enter fullscreen mode Exit fullscreen mode

Create database

Create a sample database using the command:

createdb sampledb
Enter fullscreen mode Exit fullscreen mode

Connect to the database

Run this command to connect to the database you just created.

psql sampledb
psql (15.10 (Homebrew), server 14.15 (Homebrew))
Type "help" for help.

sampledb=#
Enter fullscreen mode Exit fullscreen mode

Create Table

Once connected, create a table in your database:

sampledb=# CREATE TABLE users (
    user_id SERIAL PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    password_hash TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Enter fullscreen mode Exit fullscreen mode

Installing Claude Desktop

Claude Desktop is a desktop application developed by Anthropic that brings the capabilities of their AI chatbot, Claude, directly to your computer. Available for both macOS and Windows, the app offers a seamless integration of Claude's features into your daily workflow, enhancing productivity and accessibility.
CLAUDE

Key Features:

  • Instant Access: Launch Claude directly from your desktop without the need to navigate to a web browser, streamlining your workflow.

  • Enhanced Performance: Designed for deep work, the desktop app provides a faster and more focused experience compared to the web interface.

  • Voice Interaction: Anthropic has introduced dictation support in its mobile apps, allowing users to interact with Claude using voice commands. This feature enhances accessibility and user experience.

  • Cross-Platform Availability: Claude Desktop is available for both macOS and Windows, ensuring compatibility with a wide range of systems.

Download and install Claude Desktop using this link

Image1

Configure Claude Desktop Configuration

Once installed, follow these steps to configure Claude Desktop for PostgreSQL.

  • Open Applications > Claude Desktop.
  • Click on "Edit" Config.
  • Add the following entry for PostgreSQL in the configuration:

Image5

Once downloaded, open Applications > Claude Desktop

Click "Edit Config" and add the following entry for Postgres

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://localhost/sampledb"
      ]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Restart the Claude Desktop and click on Hammer sign shown under the UI.

Image9

Image1

Querying the Database

With the setup complete, you can now start querying your PostgreSQL database directly through Claude Desktop. Use the interface to execute SQL commands and manage your data effectively.

With these steps, your PostgreSQL database and Claude Desktop are ready to handle your projects seamlessly. Happy coding!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay