Skip to main content
Glama
@yawlabs/postgres-mcp

@yawlabs/postgres-mcp

by YawLabs

Run SQL query

pg_query
Destructive

Run parameterized SQL against a configured PostgreSQL database with read-only safeguards by default and row-limit truncation for large results.

Instructions

Run a SQL query against the configured PostgreSQL database. Postgres itself is the primary safety gate: the role in DATABASE_URL enforces what queries can succeed. The recommended posture is a least-privileged role (e.g. one granted pg_read_all_data), which makes writes server-rejected regardless of any env var. ALLOW_WRITES=1 is a secondary belt-and-braces gate - it lifts the in-server BEGIN READ ONLY wrapper, but it cannot grant privileges the role lacks. Useful for managed databases where creating a second role is awkward. Without ALLOW_WRITES this applies the same statement checks as pg_readonly, which stays read-only whatever the flag says. Use params for parameterized queries to avoid SQL injection. Params can be strings, numbers, booleans, null, arrays (for postgres arrays / ANY), or objects (for json/jsonb columns). Dates and UUIDs can be passed as ISO strings. Large result sets are truncated to POSTGRES_MAX_ROWS (default 1000) with a truncated: true flag; a result over POSTGRES_MAX_RESULT_BYTES fails instead. Session state does not carry over between calls.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "sql": "<sql>"
    },
    "name": "pg_query"
  }
}

Input Schema

NameRequiredDescriptionDefault
sqlYesThe SQL statement to execute. Hard cap of 1 MB.
paramsNoPositional parameters referenced as $1, $2, ... in the SQL.

Output Schema

NameRequiredDescriptionDefault
rowsYesResult rows, capped at POSTGRES_MAX_ROWS. Values are whatever JSON type pg parsed the column into.
fieldsYesResult column descriptors, in select-list order.
commandNoPostgres command tag (`INSERT`, `CREATE TABLE`, ...). Absent on the cursor path -- read absence as 'row-returning statement, command unknown'.
rowCountYesRows AFFECTED for DML -- not necessarily rows.length -- and rows returned on the cursor path. Null when pg reported no count.
truncatedNoPresent and true only when the result hit POSTGRES_MAX_ROWS and rows were dropped.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed2 schema fields changedv0.12.1
    • removedOutput schema / properties / rowCount / anyOf
      Removed value: -[
      -  {
      -    "type": "number"
      -  },
      -  {
      -    "type": "null"
      -  }
      -]
    • addedOutput schema / properties / rowCount / type
      Added value: +[
      +  "number",
      +  "null"
      +]
  2. Changed5 schema fields changedv0.12.0
    • addedInput schema / $defs
      Added value: +{
      +  "__schema0": {
      +    "anyOf": [
      +      {
      +        "type": "string"
      +      },
      +      {
      +        "type": "number"
      +      },
      +      {
      +        "type": "boolean"
      +      },
      +      {
      +        "type": "null"
      +      },
      +      {
      +        "items": {
      +          "$ref": "#/$defs/__schema0"
      +        },
      +        "type": "array"
      +      },
      +      {
      +        "additionalProperties": {
      +          "$ref": "#/$defs/__schema0"
      +        },
      +        "propertyNames": {
      +          "type": "string"
      +        },
      +        "type": "object"
      +      }
      +    ]
      +  }
      +}
    • changedInput schema / $schema
      Previous value: -"http://json-schema.org/draft-07/schema#"New value: +"https://json-schema.org/draft/2020-12/schema"
    • removedInput schema / definitions
      Removed value: -{
      -  "__schema0": {
      -    "anyOf": [
      -      {
      -        "type": "string"
      -      },
      -      {
      -        "type": "number"
      -      },
      -      {
      -        "type": "boolean"
      -      },
      -      {
      -        "type": "null"
      -      },
      -      {
      -        "items": {
      -          "$ref": "#/definitions/__schema0"
      -        },
      -        "type": "array"
      -      },
      -      {
      -        "additionalProperties": {
      -          "$ref": "#/definitions/__schema0"
      -        },
      -        "propertyNames": {
      -          "type": "string"
      -        },
      -        "type": "object"
      -      }
      -    ]
      -  }
      -}
    • changedInput schema / properties / params / items / $ref
      Previous value: -"#/definitions/__schema0"New value: +"#/$defs/__schema0"
    • changedOutput schema / (root)
      Previous value: -nullNew value: +{
      +  "$schema": "https://json-schema.org/draft/2020-12/schema",
      +  "additionalProperties": false,
      +  "properties": {
      +    "command": {
      +      "description": "Postgres command tag (`INSERT`, `CREATE TABLE`, ...). Absent on the cursor path -- read absence as 'row-returning statement, command unknown'.",
      +      "type": "string"
      +    },
      +    "fields": {
      +      "description": "Result column descriptors, in select-list order.",
      +      "items": {
      +        "additionalProperties": false,
      +        "properties": {
      +          "dataTypeID": {
      +            "type": "number"
      +          },
      +          "dataTypeName": {
      +            "type": "string"
      +          },
      +          "name": {
      +            "type": "string"
      +          }
      +        },
      +        "required": [
      +          "name",
      +          "dataTypeID"
      +        ],
      +        "type": "object"
      +      },
      +      "type": "array"
      +    },
      +    "rowCount": {
      +      "anyOf": [
      +        {
      +          "type": "number"
      +        },
      +        {
      +          "type": "null"
      +        }
      +      ],
      +      "description": "Rows AFFECTED for DML -- not necessarily rows.length -- and rows returned on the cursor path. Null when pg reported no count."
      +    },
      +    "rows": {
      +      "description": "Result rows, capped at POSTGRES_MAX_ROWS. Values are whatever JSON type pg parsed the column into.",
      +      "items": {
      +        "additionalProperties": {},
      +        "propertyNames": {
      +          "type": "string"
      +        },
      +        "type": "object"
      +      },
      +      "type": "array"
      +    },
      +    "truncated": {
      +      "description": "Present and true only when the result hit POSTGRES_MAX_ROWS and rows were dropped.",
      +      "type": "boolean"
      +    }
      +  },
      +  "required": [
      +    "rows",
      +    "rowCount",
      +    "fields"
      +  ],
      +  "type": "object"
      +}
  3. First observedv0.7.0

TDQS

A4.6/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Goes well beyond the annotations (readOnlyHint=false, destructiveHint=true) by explaining the actual enforcement model: the DATABASE_URL role is the primary gate, ALLOW_WRITES only lifts the server-side BEGIN READ ONLY wrapper and cannot grant missing privileges, and params must be used to avoid injection. It also discloses truncation at POSTGRES_MAX_ROWS (default 1000 with `truncated: true`), hard failure above POSTGRES_MAX_RESULT_BYTES, and that session state does not persist between calls.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Front-loads the core action, then layers safety model, param guidance, and limits in a logical order with no filler or repetition. The env-var philosophy (least-privileged role vs. ALLOW_WRITES) is slightly verbose but each sentence carries operational information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a destructive, open-world SQL execution tool, it covers the safety model, injection avoidance, result-size behavior, and session lifetime. An output schema exists, so not restating return shapes is appropriate, and the truncation note still flags the one result behavior an agent must anticipate.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so the baseline is 3, but the description earns more by explaining accepted param value types (strings, numbers, booleans, null, arrays for postgres arrays/ANY, objects for json/jsonb) and that dates and UUIDs can be passed as ISO strings. That is semantic guidance the recursive $defs schema cannot convey.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

States a specific verb and resource ('Run a SQL query against the configured PostgreSQL database') and explicitly positions itself against the sibling pg_readonly, noting that pg_readonly 'stays read-only whatever the flag says' while this tool can write under ALLOW_WRITES. An agent can distinguish the two without opening either schema.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Gives concrete usage context: use `params` for parameterized queries to avoid injection, and use ALLOW_WRITES for managed databases where a second role is awkward. The pg_readonly comparison implies when to prefer the read-only sibling, though the routing is framed around safety gating rather than an explicit 'use X instead of Y when Z' statement.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.