PostgreSQL MCP server

Enables AI to discover, connect to, and query PostgreSQL databases with multi-database support, rich schema information, and secure read-only transactions without exposing sensitive connection details.
Back to servers
Setup instructions
Provider
stuzero
Release date
Apr 01, 2025
Language
Python
Stats
24 stars

This repository implements a server for the Model Context Protocol (MCP), which facilitates communication between models and clients while tracking and managing context efficiently. The server is designed to work with PostgreSQL for storing model states and context data.

Installation

To install the MCP server, you'll need to follow these steps:

Prerequisites

  • Node.js (14.x or higher)
  • PostgreSQL database

Setting Up the Server

  1. Clone the repository:
git clone https://github.com/stuzero/pg-mcp-server.git
cd pg-mcp-server
  1. Install dependencies:
npm install
  1. Configure your PostgreSQL connection. Create a .env file in the root directory with the following variables:
DATABASE_URL=postgresql://username:password@localhost:5432/mcp_database
PORT=3000
  1. Initialize the database schema:
npm run db:init

Usage

Starting the Server

To start the MCP server in development mode:

npm run dev

For production deployment:

npm run build
npm start

Connecting Clients

Clients can connect to the MCP server using a compatible client library. The official client can be found at https://github.com/stuzero/pg-mcp-client.

API Endpoints

The server exposes several endpoints for client-model interaction:

Context Management

POST /context/create
GET /context/:id
PUT /context/:id
DELETE /context/:id

Model Interaction

POST /model/query
POST /model/stream

Configuration Options

You can customize the server behavior by modifying the following environment variables:

  • PORT: The port on which the server listens (default: 3000)
  • DATABASE_URL: PostgreSQL connection string
  • LOG_LEVEL: Logging verbosity (debug, info, warn, error)
  • MAX_CONTEXT_SIZE: Maximum size of stored context in tokens
  • MODEL_TIMEOUT: Timeout for model requests in milliseconds

Example Usage

Here's a basic example of using the MCP server with curl:

# Create a new context
curl -X POST http://localhost:3000/context/create \
  -H "Content-Type: application/json" \
  -d '{"modelId": "gpt-4", "systemPrompt": "You are a helpful assistant."}'

# Query the model with context
curl -X POST http://localhost:3000/model/query \
  -H "Content-Type: application/json" \
  -d '{
    "contextId": "your-context-id",
    "message": "What is the capital of France?",
    "options": {"temperature": 0.7}
  }'

Troubleshooting

Common Issues

  • Database Connection Errors: Verify your PostgreSQL credentials and ensure the database is running
  • Permission Denied: Check file permissions on the .env file and database access
  • Port Already in Use: Change the PORT in your .env file if the default port is already occupied

If you encounter issues, check the server logs for more detailed information about the error.

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "pg-mcp-server" '{"command":"npx","args":["pg-mcp-server"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "pg-mcp-server": {
            "command": "npx",
            "args": [
                "pg-mcp-server"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "pg-mcp-server": {
            "command": "npx",
            "args": [
                "pg-mcp-server"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later