Skip to main content

A Model Context Protocol server for PostgreSQL with read-only default and super-code write access.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgsql_mcp-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgsql_mcp-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pgsql_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: pgsql_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pgsql_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cdda21efe57e6c793c5b63b0e5715e04547c64052273cf9787f5bec743822910
MD5 8f23a56e0e34986510458b3335588f6d
BLAKE2b-256 2a66840157f051973beb50fd7f2e278d71d5846ef425ef7dbbf74c3a4a56a58a

See more details on using hashes here.

File details

Details for the file pgsql_mcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pgsql_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pgsql_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc1a4b2c4b27f827aacbe895a39f018bebd7fe7d6a8de073763b80933ee16b33
MD5 cc4742f1794c6d6fcfa845169e4cbf09
BLAKE2b-256 30933146dff9d952dc4962391846222b0e3f5e70c8a5d1aa33733ba0e05faa28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page