Skip to main content

pgsql-mcp

A Model Context Protocol (MCP) server for PostgreSQL. Provides read-only database access by default, with optional write operations gated behind a super code.

Overview

pgsql-mcp exposes PostgreSQL databases to MCP-compatible AI clients (such as Claude, Kiro, and Cursor) through a set of structured tools. It supports schema inspection, table browsing, row fetching, arbitrary query execution, and stored function/procedure invocation.

Write and DDL operations are disabled unless a super code is provided at server startup. This ensures safe, read-only access in environments where unrestricted database modification is not desired.

Installation

pip install pgsql-mcp

Usage

Start the server with a PostgreSQL DSN:

# Read-only mode (default)
pgsql-mcp --dsn "postgresql://user:password@localhost:5432/mydb"

# With write access enabled
pgsql-mcp --dsn "postgresql://user:password@localhost:5432/mydb" --super-code "your-secret"

Command-Line Arguments

Argument Required Description
--dsn Yes PostgreSQL connection string in DSN format
--super-code No Secret code to unlock write and DDL operations

MCP Client Configuration

Add the following to your MCP client configuration file:

{
  "mcpServers": {
    "postgres": {
      "command": "pgsql-mcp",
      "args": [
        "--dsn", "postgresql://user:password@localhost:5432/mydb"
      ],
      "autoApprove": [
        "list_schemas",
        "list_tables",
        "describe_table",
        "fetch_rows",
        "run_query",
        "call_function"
      ]
    }
  }
}

Tools

list_schemas

List all non-system schemas in the database.

Parameters: None

list_tables

List all tables in a given schema.

Parameter Type Default Description
schema string "public" Schema name

describe_table

Describe columns of a table, including name, data type, nullability, and default value.

Parameter Type Default Description
table string required Table name
schema string "public" Schema name

fetch_rows

Fetch rows from a table with optional column selection, WHERE clause, and row limit.

Parameter Type Default Description
table string required Table name
columns string "*" Comma-separated column names or "*"
where string None SQL WHERE clause (without the WHERE keyword)
limit integer 100 Maximum number of rows to return
schema string "public" Schema name

run_query

Execute an arbitrary SQL query. Read queries are permitted without authentication. Write and DDL queries require the super code.

Parameter Type Default Description
sql string required SQL statement to execute
code string None Super code for write operations

call_function

Call a PostgreSQL function or procedure and return its result. Functions are invoked with SELECT; procedures are invoked with CALL.

Parameter Type Default Description
function_name string required Name of the function or procedure
args list None List of arguments (passed as literal strings)
is_procedure boolean false Use CALL instead of SELECT
schema string "public" Schema name

Security Model

  • By default, the server operates in read-only mode. All SELECT queries, schema inspection, and function calls are permitted.
  • Write operations (INSERT, UPDATE, DELETE) and DDL statements (CREATE, ALTER, DROP, TRUNCATE, GRANT, REVOKE) are blocked unless the server is started with --super-code.
  • When write access is enabled, the caller must pass the matching code in the run_query tool's code parameter.

Requirements

  • Python 3.12 or later
  • PostgreSQL database accessible via the provided DSN
  • Dependencies: mcp[cli], psycopg[binary]

License

MIT

Release files for pgsql-mcp 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgsql-mcp 0.1.0
File Size Uploaded
pgsql_mcp-0.1.0.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pgsql-mcp 0.1.0
File Interpreter ABI Platform
pgsql_mcp-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / pgsql_mcp-0.1.0.tar.gz

Download URL pgsql_mcp-0.1.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cdda21efe57e6c793c5b63b0e5715e04547c64052273cf9787f5bec743822910
BLAKE2b-256 checksum
How to use checksums
2a66840157f051973beb50fd7f2e278d71d5846ef425ef7dbbf74c3a4a56a58a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / pgsql_mcp-0.1.0-py3-none-any.whl

Download URL pgsql_mcp-0.1.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc1a4b2c4b27f827aacbe895a39f018bebd7fe7d6a8de073763b80933ee16b33
BLAKE2b-256 checksum
How to use checksums
30933146dff9d952dc4962391846222b0e3f5e70c8a5d1aa33733ba0e05faa28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page