PostgreSQL MCP Server

An MCP server that exposes PostgreSQL database operations as tools for AI

README.md

PostgreSQL MCP Server

An MCP server that exposes PostgreSQL database operations as tools for AI assistants.

What is MCP?

Model Context Protocol (MCP) connects AI assistants to external tools and data. This server lets AI assistants execute SQL queries and inspect your PostgreSQL database schema.

Setup

Python version: This project currently supports Python 3.10–3.13. Python 3.14 is excluded due to upstream dependency wheel support (e.g., psycopg2-binary and pydantic-core).

1. Install Dependencies

Note: This project uses Poetry for dependency management. If you don't have Poetry installed, you can install it with:

curl -sSL https://install.python-poetry.org | python3 -

See the official Poetry documentation for alternative installation methods.

poetry install

2. Configure Database

Copy .env.example to .env and add your PostgreSQL credentials:

cp .env.example .env

Edit .env:

DB_NAME=your_database
DB_USER=postgres
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432

Testing

MCP Inspector (Recommended)

Note: The Inspector runs via npx, so you need Node.js (which includes npm). If you don't have it installed, get it from the official Node.js installer.

npx @modelcontextprotocol/inspector poetry run python postgres-mcp-server/main.py

This opens a web UI where you can:

  • View available tools under the Tools tab
  • Test get_schema
  • See real-time results

Quick Test

poetry run python postgres-mcp-server/main.py

Press Ctrl+C to stop. No errors = working correctly.

Available Tools

get_schema() - Get database schema

Connect to Cursor

Add to your Cursor MCP config (global settings):

{
  "mcpServers": {
    "postgres": {
      "command": "poetry",
      "args": ["-C", "/absolute/path/to/postgres-mcp-server", "run", "python", "postgres-mcp-server/main.py"]
    }
  }
}

Replace /absolute/path/to/postgres-mcp-server with your actual project path.


Future Proof Data Science - Teaching data scientists to optimize workflows with AI

Tools 1

get_schemaRetrieves the database schema to allow the AI to understand the structure of your PostgreSQL database.

Environment Variables

DB_NAMErequiredThe name of the PostgreSQL database
DB_USERrequiredThe username for database authentication
DB_PASSWORDrequiredThe password for database authentication
DB_HOSTrequiredThe host address of the database server
DB_PORTrequiredThe port number for the database connection

Try it

Can you retrieve the database schema so I can understand the table structures?
What tables are currently available in my PostgreSQL database?
Help me understand the relationships between the tables in my database schema.

Frequently Asked Questions

What are the key features of PostgreSQL MCP Server?

Exposes PostgreSQL database operations as AI tools. Allows AI assistants to inspect database schemas. Supports execution of SQL queries. Integrates directly with MCP-compatible AI clients.

What can I use PostgreSQL MCP Server for?

Quickly exploring database table structures without manual SQL queries. Assisting developers in writing complex SQL queries by providing schema context. Automating database documentation tasks by retrieving schema information.

How do I install PostgreSQL MCP Server?

Install PostgreSQL MCP Server by running: poetry install

What MCP clients work with PostgreSQL MCP Server?

PostgreSQL MCP Server works with any MCP-compatible client including Claude Desktop, Claude Code, Cursor, and other editors with MCP support.

Turn this server into reusable context

Keep PostgreSQL MCP Server docs, env vars, and workflow notes in Conare so your agent carries them across sessions.

Open Conare