home / mcp / postgresql mcp server

PostgreSQL MCP Server

Exposes PostgreSQL database operations as MCP tools for AI assistants.

Installation
Add the following to your MCP client configuration file.

Configuration

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

You can run a PostgreSQL MCP Server to expose SQL operations as accessible tools for AI assistants. It enables assistants to query your database and inspect its schema, making database interactions safer and AI-driven workflows easier to automate.

How to use

You interact with the PostgreSQL MCP Server by running it locally and connecting through an MCP client. Start the server using the provided command, then configure your MCP client to point at the local runtime. Once connected, you can invoke the available tools to query the database and retrieve schema details. Use the server to execute SQL queries, inspect tables, views, and columns, and test how your AI assistant can leverage live data from PostgreSQL.

How to install

Prerequisites: you need Python and Poetry installed on your machine.

Install dependencies with Poetry.

poetry install

Create the environment file from the example and provide your database credentials.

cp .env.example .env
```
Then edit the .env file to include your database connection details.

The server can then be started directly from the project using Poetry.

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

Additional sections

Configuration and runtime notes are shown in the local project setup. If you need to connect through an MCP client, use the stdio configuration that launches the server via Poetry and runs the main Python script.

For testing and quick validation, you can run an inspector to view available tools and test schema retrieval by launching the server in inspector mode, which opens a web UI for real-time results.

npx @modelcontextprotocol/inspector poetry run python postgres-mcp-server/main.py
```
This opens a web UI where you can view tools, test get_schema, and observe results in real time.

Available tools

get_schema

Retrieve the database schema including tables, views, columns, and relationships to understand the structure of your PostgreSQL database.