Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Postgres and Model Context Protocol

2 min read

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:

  • 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

Start Postgres Service

After installation, start the PostgreSQL service:

brew services start postgresql

Result:

 brew services start postgresql
==> Successfully started `postgresql@14` (lab

Create database

Create a sample database using the command:

createdb sampledb

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=#

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
);

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.

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

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:

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"
      ]
    }
  }
}

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

You will find that it successfully detected your Postgres along with the table name from your local system.

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. In my next blog article, I will show you how to integrate Postgres running in a Docker container with the Postgres MCP server and Claude Desktop. Happy coding!

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Leave a Reply

Join our Discord Server
Index