Skip to main content
Glama
postgres-mcp-server

postgres-mcp-server

by rajath002

PostgreSQL MCP Server

An enterprise-ready Model Context Protocol (MCP) server built with Node.js & TypeScript for PostgreSQL databases. Compatible with Antigravity, Claude Desktop, Cursor, Continue, and any standard MCP client.


๐ŸŒŸ Features & Tools Exposed

Tool Name

Mode

Description

list_tables

Read-only

Lists all schemas, tables, views, estimated row counts, and table disk sizes.

describe_table

Read-only

Details table schema: column types, nullability, defaults, primary keys, foreign keys, and indexes.

read_query

Read-only

Executes SELECT queries safely inside DB-level READ ONLY transactions with auto-limiting.

explain_query

Read-only

Generates PostgreSQL query execution plans (EXPLAIN / EXPLAIN ANALYZE) in JSON format.

get_db_stats

Read-only

Returns DB health stats: database size, top 10 largest tables, active connections, and cache hit ratios.

execute_query

Write (Optional)

Executes INSERT, UPDATE, DELETE, or DDL statements (disabled unless ALLOW_WRITE_QUERIES=true).


Related MCP server: PostgreSQL MCP Server

๐Ÿ› ๏ธ Quick Start

1. Install Dependencies & Build

npm install
npm run build

2. Configure Environment Variables

Copy .env.example to .env and fill in your PostgreSQL credentials:

# Database Connection String
DATABASE_URL=postgresql://postgres:password@localhost:5432/mydb

# OR individual settings:
PGHOST=localhost
PGPORT=5432
PGUSER=postgres
PGPASSWORD=your_password
PGDATABASE=mydb

# Enable write capabilities if needed (Default: false)
ALLOW_WRITE_QUERIES=false

๐Ÿš€ Registering with MCP Clients

This server uses standard stdio communication and can be added to any MCP-compliant application.

1. Antigravity Configuration

Add to your Antigravity MCP settings:

  • Global Config: %USERPROFILE%\.gemini\antigravity-cli\mcp_config.json (Windows) or ~/.gemini/antigravity-cli/mcp_config.json (Linux/macOS)

  • Workspace Config: .agents/mcp.json

{
  "mcpServers": {
    "postgres-db": {
      "command": "node",
      "args": [
        "D:/Projects/AI/MCP/postgres-connector/dist/index.js"
      ],
      "env": {
        "DATABASE_URL": "postgresql://your_user:your_password@localhost:5432/your_project_db",
        "ALLOW_WRITE_QUERIES": "false"
      }
    }
  }
}

2. Claude Desktop Configuration

Add to your Claude Desktop configuration file:

  • Windows: %APPDATA%\Claude\claude_desktop_config.json

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "postgres-db": {
      "command": "node",
      "args": [
        "D:/Projects/AI/MCP/postgres-connector/dist/index.js"
      ],
      "env": {
        "DATABASE_URL": "postgresql://your_user:your_password@localhost:5432/your_project_db",
        "ALLOW_WRITE_QUERIES": "false"
      }
    }
  }
}

3. Cursor / Continue / Standard MCP Clients

For Cursor, Continue, or any other MCP host supporting stdio servers:

{
  "mcpServers": {
    "postgres-db": {
      "command": "node",
      "args": [
        "D:/Projects/AI/MCP/postgres-connector/dist/index.js"
      ],
      "env": {
        "DATABASE_URL": "postgresql://your_user:your_password@localhost:5432/your_project_db",
        "ALLOW_WRITE_QUERIES": "false"
      }
    }
  }
}

Tip (Development Mode): You can also run the server directly with npx tsx without a build step:

"command": "npx",
"args": [
  "-y",
  "tsx",
  "D:/Projects/AI/MCP/postgres-connector/src/index.ts"
]

๐Ÿงช Testing & Local Debugging

The official MCP Inspector allows you to visually trigger tool calls (list_tables, read_query, describe_table, etc.) and view response payloads in your browser:

  • Quick development testing (no compilation step required):

    npx @modelcontextprotocol/inspector npx tsx src/index.ts
  • Production build testing:

    npm run build
    npx @modelcontextprotocol/inspector node dist/index.js

The command will automatically open the inspector in your default browser (e.g. http://localhost:<port>/?MCP_INSPECTOR_API_TOKEN=...), where you can inspect available tools, fill in parameters, and view responses in real time.

Option 2: Live Integration via MCP Hosts

Register the server in your AI host config (such as Antigravity or Claude Desktop) as detailed in Registering with MCP Clients, then restart your host application to test tool invocation through natural language prompts.

Option 3: Code Compilation & Type Check

Verify TypeScript types and esbuild bundle output:

npm run build

๐Ÿ”’ Security Best Practices

  • By default, all queries executed via read_query run inside BEGIN READ ONLY; ... COMMIT; transactions to prevent unexpected data mutations.

  • Write queries via execute_query are disabled by default unless ALLOW_WRITE_QUERIES=true.


๐Ÿ“„ License & Attribution

This project is licensed under the BSD 3-Clause License.

Mandatory Terms:

  • Attribution Required: You are free to use, modify, and distribute this software, provided that all copies retain the original copyright notice (Copyright (c) 2026, Rajath Kumar D) and license terms.

  • No Unauthorized Endorsement: You may not use the author's name (Rajath Kumar D) or contributors to endorse or promote derived products without prior written permission.

Available Tools

6 tools
describe_tableA

Get detailed schema breakdown for a specific table or view, including column names, data types, constraints, primary keys, foreign keys, and indexes.

Inputs: table (required), schema.

execute_queryA

Execute data modification statements (INSERT, UPDATE, DELETE, DDL). Requires ALLOW_WRITE_QUERIES=true in environment.

Inputs: sql (required).

explain_queryA

Execute EXPLAIN on a SQL query to inspect query planner cost estimates, join strategies, and index usage.

Inputs: sql (required), analyze.

get_db_statsA

Retrieve database health & performance metrics: database size, top tables by disk usage, active connections, and cache hit ratio.

Inputs: no named parameters.

list_tablesA

List all tables, views, and schemas in the PostgreSQL database along with estimated row counts and table sizes.

Inputs: schema, include_views.

read_queryA

Execute a read-only SQL query (SELECT, WITH, etc.). Strictly executed inside a DB-level READ ONLY transaction for safety.

Inputs: sql (required), limit.

Tool Schema Changelog

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

Loading tool schema changelogโ€ฆ

    TDQS

    A4.2/5.0

    Scored across 6 tools

    Disambiguation5/5

    Each tool targets a distinct function: listing tables, describing schema, executing read-only queries, explaining query plans, retrieving database stats, and executing write operations. There is no overlap or ambiguity between tool purposes.

    Naming Consistency5/5

    All tool names follow a consistent verb_noun pattern in snake_case (list_, describe_, read_, explain_, get_, execute_). The naming is uniform and predictable.

    Tool Count5/5

    With 6 tools, the set is concise and well-scoped for a PostgreSQL server, covering the essential operations without unnecessary bloat or sparse coverage.

    Completeness5/5

    The tool surface covers the full lifecycle of database interaction: discovery, schema inspection, read queries, write queries, query planning, and server statistics. No critical gaps are apparent for the intended purpose.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      B
      maintenance
      MCP server with 14 tools for PostgreSQL database operations. Query databases, explore schemas, analyze tables, with SQL injection prevention and read-only mode by default.
      14
      12
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      An open-source MCP server for PostgreSQL schema introspection and guarded read-only queries. It enables MCP clients to discover schemas, tables, columns, indexes, relationships, and safe queryable data from a configured PostgreSQL database.
      11 npm
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Read-only PostgreSQL MCP server that enables running SELECT queries, listing tables and schemas, and describing columns, with built-in protection against writes and malicious SQL attacks.
      432 npm
      MIT