home / mcp / postgresql mcp server

PostgreSQL MCP Server

Provides an MCP-enabled interface to PostgreSQL for querying data and inspecting database structures.

Installation
Add the following to your MCP client configuration file.

Configuration

View docs
{
  "mcpServers": {
    "gldc-mcp-postgres": {
      "command": "/path/to/venv/bin/python",
      "args": [
        "/path/to/postgres_server.py"
      ],
      "env": {
        "POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database"
      }
    }
  }
}

You can run a PostgreSQL MCP Server to let AI agents interact with your PostgreSQL databases through a standardized MCP interface. It exposes tools to explore schemas, tables, and query results, and it supports typed outputs for easy integration with MCP clients.

How to use

Start by running the server locally or in a container. You will connect an MCP client to the server to perform actions such as listing schemas, listing tables in a schema, describing table structures, retrieving foreign keys, and executing SQL queries. Typical workflows include inspecting a database structure before running analytics queries, and then executing read-only queries to fetch data in structured formats.

How to install

Prerequisites you need on your machine:

  • Python 3.8+ installed
  • Virtual environment support (recommended)
  • Git (for cloning the project)
  • Internet access to install Python dependencies

Follow these concrete steps to set up and run the server:

# 1) Create and activate a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows, use: venv\Scripts\activate

# 2) Install dependencies
pip install -r requirements.txt

Available tools

query

Execute SQL queries against the database. Supports parameters and row limits and can output in markdown or json formats.

list_schemas

List all available schemas in the connected database, with optional filters and pagination.

list_tables

List tables within a specific schema, with optional name pattern filtering and table types.

describe_table

Provide detailed information about a table’s columns, types, and constraints.

get_foreign_keys

Retrieve foreign key relationships for a given table to understand cross-table references.

find_relationships

Discover both explicit and implied relationships for a table to aid data modeling.

db_identity

Show current database identity information such as user, host, port, and search_path.

run_query

Typed tool to execute a SQL query with inputs for sql, parameters, row_limit, and output format.

run_query_json

Execute a query and return results as JSON-serializable rows.

list_schemas_json

Return schemas with filters for include_system, include_temp, require_usage, and row_limit.

list_tables_json

Return tables within a schema with filters for name pattern, case sensitivity, and row_limit.

table resources

Optional reading of table resources via table:// URIs when supported.

prompts

Prompts registered for safe query construction and plan tips when supported.