Skip to main content
Glama
mcp-postgres

mcp-postgres

by gbfeliped

mcp-postgres

Read-only PostgreSQL MCP server for Claude Desktop (and any MCP-compatible client).

Tools exposed

Tool

Description

query

Run a SELECT query

list_tables

List all user tables with row-count estimate and size

describe_table

Show columns, types, nullability, PK for a table

list_schemas

List user-defined schemas

Related MCP server: postgres-mcp-readonly

Security protections

Attack

Example

Status

Direct writes

DROP, DELETE, UPDATE, INSERT, COPY

✅ Blocked

Multiple statements

SELECT 1; DROP TABLE t

✅ Blocked

System functions

pg_read_file, pg_sleep, dblink

✅ Blocked

DoS

pg_sleep(10), GENERATE_SERIES(1,99999999)

✅ Blocked

Dollar-quoted bypass

$$DROP TABLE t$$

✅ Blocked

Whitespace bypass

tabs and newlines between keywords

✅ Blocked

Comment bypass

/* */ and -- before keywords

✅ Blocked

Session write mode

Enforced via READ ONLY transaction

✅ Enforced

Valid queries

SELECT, SELECT ... LIMIT, COUNT, WHERE

✅ Allowed

Environment variables

PostgreSQL connection

Variable

Required

Default

Description

PG_HOST

✅

—

Server hostname or IP

PG_DATABASE

✅

—

Database name

PG_USER

✅

—

PostgreSQL username

PG_PASSWORD

✅

—

PostgreSQL password

PG_PORT

5432

TCP port

PG_SSL

false

Enable SSL (true/false)

PG_SSL_REJECT_UNAUTHORIZED

true

Reject self-signed certs

SSH tunnel (optional)

Set PG_SSH_HOST to activate the tunnel. Authentication requires either a private key file or a password.

Variable

Required

Default

Description

PG_SSH_HOST

—

—

SSH server hostname (activates tunnel when set)

PG_SSH_PORT

22

SSH server port

PG_SSH_USER

✅ (if tunnel)

—

SSH username

PG_SSH_PRIVATE_KEY

✅ or password

—

Path to private key file (e.g. ~/.ssh/id_rsa)

PG_SSH_KEY_PASSPHRASE

—

Passphrase for the private key (if encrypted)

PG_SSH_PASSWORD

✅ or key

—

SSH password (used when no key file is provided)

PG_SSH_REMOTE_HOST

PG_HOST

Postgres host as seen from the SSH server

Build

npm install
npm run build

Claude Desktop configuration

Add to %APPDATA%\Claude\claude_desktop_config.json:

Conexão direta:

{
  "mcpServers": {
    "postgres": {
      "command": "node",
      "args": ["C:/Projetos/mcp-postgres-readonly/dist/index.js"],
      "env": {
        "PG_HOST": "localhost",
        "PG_DATABASE": "mydb",
        "PG_USER": "postgres",
        "PG_PASSWORD": "yourpassword"
      }
    }
  }
}

Conexão direta com SSL (certificado auto-assinado):

{
  "mcpServers": {
    "postgres": {
      "command": "node",
      "args": ["C:/Projetos/mcp-postgres-readonly/dist/index.js"],
      "env": {
        "PG_HOST": "db.example.com",
        "PG_DATABASE": "mydb",
        "PG_USER": "postgres",
        "PG_PASSWORD": "yourpassword",
        "PG_SSL": "true",
        "PG_SSL_REJECT_UNAUTHORIZED": "false"
      }
    }
  }
}

Via SSH tunnel (private key):

{
  "mcpServers": {
    "postgres": {
      "command": "node",
      "args": ["C:/Projetos/mcp-postgres-readonly/dist/index.js"],
      "env": {
        "PG_HOST": "localhost",
        "PG_DATABASE": "mydb",
        "PG_USER": "postgres",
        "PG_PASSWORD": "yourpassword",
        "PG_SSH_HOST": "bastion.example.com",
        "PG_SSH_USER": "ubuntu",
        "PG_SSH_PRIVATE_KEY": "C:/Users/you/.ssh/id_rsa"
      }
    }
  }
}

Via SSH tunnel (password):

{
  "mcpServers": {
    "postgres": {
      "command": "node",
      "args": ["C:/Projetos/mcp-postgres-readonly/dist/index.js"],
      "env": {
        "PG_HOST": "localhost",
        "PG_DATABASE": "mydb",
        "PG_USER": "postgres",
        "PG_PASSWORD": "yourpassword",
        "PG_SSH_HOST": "bastion.example.com",
        "PG_SSH_USER": "ubuntu",
        "PG_SSH_PASSWORD": "sshpassword"
      }
    }
  }
}

Tip: When PG_SSH_REMOTE_HOST is omitted, the tunnel forwards to PG_HOST as seen from the SSH server. If Postgres runs on a private address only reachable from the bastion (e.g. db.internal), set PG_SSH_REMOTE_HOST=db.internal and PG_HOST to anything (it will be overridden).

Available Tools

4 tools
describe_tableA

Describe the columns of a PostgreSQL table: name, data type, nullability, default value, and whether it is part of the primary key.

Inputs: table (required), schema.

list_schemasA

List all user-defined schemas in the connected PostgreSQL database.

Inputs: no named parameters.

list_tablesA

List all user tables in the connected PostgreSQL database, including schema name, table name, row count estimate, and size.

Inputs: no named parameters.

queryA

Execute a read-only SELECT query against PostgreSQL. Only SELECT (and CTEs starting with WITH) are permitted. Write operations, system functions, DoS constructs, and multiple statements are all blocked.

Inputs: sql (required).

Tool Schema Changelog

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

Loading tool schema changelog…

    TDQS

    A3.7/5.0

    Scored across 4 tools

    Disambiguation4/5

    The three metadata tools (list_tables, describe_table, list_schemas) target clearly distinct objects, and query is obviously the general-purpose execution tool. The only mild overlap is that query could technically satisfy anything the metadata helpers do, but the read-only SELECT restriction keeps boundaries reasonably clear.

    Naming Consistency4/5

    list_tables, describe_table, and list_schemas follow a clear verb_noun lowercase-snake pattern. The bare verb 'query' breaks the pattern slightly, but it remains readable and the deviation is minor.

    Tool Count4/5

    Four tools is a reasonable, tightly scoped surface for read-only PostgreSQL access, with each tool earning its place. It sits at the thin end of the ideal range, though the small set is appropriate for a deliberately read-only server.

    Completeness4/5

    The surface covers the core read-only workflows: running queries, discovering schemas, listing tables, and inspecting columns. Minor gaps exist (no view/index/constraint inspection, no query-plan/EXPLAIN helper), and write operations are intentionally absent, but agents can work around most of these via query.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      B
      maintenance
      A read-only PostgreSQL MCP server that enables AI agents to perform schema introspection and execute SELECT-only queries. It supports secure database connections through SSL and SSH tunnels while offering a structure-only mode to restrict query access.
      146 npm
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Read-only MCP server for PostgreSQL, enabling schema discovery, table metadata, and safe SELECT queries via READ ONLY transactions.
      MIT
    • F
      license
      Not graded
      quality
      B
      maintenance
      Read-only MCP server for PostgreSQL enabling schema introspection and SELECT queries via MCP clients like Claude, with multi-layered write protection.
      -