Skip to main content
Glama
PostgreSQL Read-Only MCP Server

PostgreSQL Read-Only MCP Server

PostgreSQL Read-Only MCP Server

A Model Context Protocol (MCP) server that provides safe, read-only access to one configured PostgreSQL database.

The server exposes schema inspection, data preview, custom SELECT query execution, relationship discovery, database statistics, safe connection metadata, and a connection_info prompt for MCP clients such as Claude Desktop.

Features

  • Single database per MCP server instance

  • Strict PostgreSQL read-only validation (SELECT only)

  • Schema-aware tools with optional schema input, defaulting to public

  • Server-side row limits and query timeout protection

  • Data preview with optional column selection, basic where filtering, and text truncation

  • Foreign key relationship discovery

  • Database statistics with largest tables by rows and by size

  • Safe db_info output that never exposes passwords

  • connection_info MCP prompt with connection context and usage guidance

Related MCP server: PostgreSQL MCP Server

Installation

Use directly with npx in your MCP client config:

npx -y postgres-readonly-mcp

Or install globally:

npm install -g postgres-readonly-mcp
postgres-readonly-mcp

For local development only:

git clone https://github.com/beydemirfurkan/postgres-readonly-mcp.git
cd postgres-readonly-mcp
npm install
npm run build

Claude Desktop Config

Recommended npx setup:

{
  "mcpServers": {
    "postgres-readonly": {
      "command": "npx",
      "args": ["-y", "postgres-readonly-mcp"],
      "env": {
        "POSTGRES_HOST": "127.0.0.1",
        "POSTGRES_PORT": "5432",
        "POSTGRES_USER": "postgres",
        "POSTGRES_PASSWORD": "your_password",
        "POSTGRES_DATABASE": "your_database",
        "POSTGRES_SSL": "true",
        "POSTGRES_SSL_REJECT_UNAUTHORIZED": "true"
      }
    }
  }
}

Connection string setup:

{
  "mcpServers": {
    "postgres-readonly": {
      "command": "npx",
      "args": ["-y", "postgres-readonly-mcp"],
      "env": {
        "DATABASE_URL": "postgresql://USER:PASSWORD@HOST:5432/DB_NAME",
        "POSTGRES_SSL": "true"
      }
    }
  }
}

Restart Claude Desktop after editing the config.

Multiple Databases

One server instance maps to one PostgreSQL database. To expose multiple databases, add multiple MCP server entries with different names and environment variables:

{
  "mcpServers": {
    "postgres-crm": {
      "command": "npx",
      "args": ["-y", "postgres-readonly-mcp"],
      "env": {
        "DATABASE_URL": "postgresql://USER:PASSWORD@HOST:5432/crm"
      }
    },
    "postgres-analytics": {
      "command": "npx",
      "args": ["-y", "postgres-readonly-mcp"],
      "env": {
        "DATABASE_URL": "postgresql://USER:PASSWORD@HOST:5432/analytics"
      }
    }
  }
}

Environment Variables

  • POSTGRES_HOST: PostgreSQL host, default localhost

  • POSTGRES_PORT: PostgreSQL port, default 5432

  • POSTGRES_USER: PostgreSQL user, default postgres

  • POSTGRES_PASSWORD: PostgreSQL password, default empty

  • POSTGRES_DATABASE: PostgreSQL database name, default postgres

  • POSTGRES_SSL: Enable TLS, default true

  • POSTGRES_SSL_REJECT_UNAUTHORIZED: Reject unauthorized TLS certificates, default true

  • POSTGRES_QUERY_TIMEOUT_MS: Query timeout in milliseconds, minimum 1000, default 30000

  • DATABASE_URL: PostgreSQL connection URL fallback

  • POSTGRES_URL: PostgreSQL connection URL alias

Explicit POSTGRES_* host fields override URL values when both are present.

If your password includes special characters (/, @, :), URL encode it in connection strings.

Tools

db_info

Returns safe connection metadata for the configured database.

Input:

{}

list_tables

Lists tables/views in a schema.

Input:

{ "schema": "public" }

describe_table

Describes columns, primary key, foreign keys, and indexes.

Input:

{
  "schema": "public",
  "table": "users"
}

preview_data

Previews rows from a table with optional column selection and basic filtering.

Input:

{
  "schema": "public",
  "table": "users",
  "columns": ["id", "email", "created_at"],
  "where": "created_at >= '2026-01-01'",
  "limit": 10
}

run_query

Runs a custom read-only SQL query.

Input:

{
  "query": "SELECT id, email FROM \"public\".\"users\" ORDER BY id DESC",
  "limit": 100
}

show_relations

Shows foreign key relations for a table.

Input:

{
  "schema": "public",
  "table": "orders"
}

db_stats

Returns database size, table count, row estimates, largestByRows, and largestBySize.

Input:

{}

Prompts

connection_info

Provides safe PostgreSQL connection context and read-only usage guidance. Passwords are never included.

Safety Model

  • Only SELECT statements are allowed

  • Multiple SQL statements in one request are blocked

  • DML, DDL, grant/revoke, maintenance, and admin keywords are blocked

  • Risky PostgreSQL functions such as pg_sleep, dblink, and file-read helpers are blocked

  • Results are capped server-side even if a query requests more rows

  • Connection/runtime errors are sanitized to avoid credential leakage

Limits

  • preview_data default: 10, max: 100

  • run_query default: 1000, max: 5000

  • Query timeout: 30000ms by default

  • Long text truncation: 200 chars

Development

npm install
npm run build
npm run dev
npm test

License

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI assistants to safely interact with PostgreSQL databases through read-only operations, providing schema discovery, table inspection, and query execution capabilities with structured context awareness.
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Provides read-only access to PostgreSQL databases, enabling users to list tables, view table structures and statistics, and execute SELECT queries safely through natural language.
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables secure read-only access to PostgreSQL databases, allowing users to list tables, query schemas, execute SELECT statements, and inspect table structures through natural language interactions.
      909 npm
      4
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI clients to explore a PostgreSQL database schema and run read-only queries, including listing tables, describing columns, executing SELECT queries, and retrieving foreign key relationships.
      ISC