ต้นแบบ · รุ่นทดลอง: มีทุก server ในทะเบียนทางการ อัปเดตทุก 30 นาที รีวิวและบัญชีผู้ใช้จะมาในขั้นถัดไป
PopNx MCPที่รวม MCP สำหรับคนใช้ AI · อธิบายเป็นภาษาคน

PostgreSQL

โดย antonorlovรุ่น 0.3.1i★ –iรันบนเครื่อง
ชื่อใน registryio.github.antonorlov/mcp-postgres-server

ติดตั้ง

ภาพรวม

คำอธิบายจากผู้ทำ (ภาษาอังกฤษตามต้นฉบับ)

MCP server for PostgreSQL: local, Docker, RDS, Neon, Supabase, or behind an SSH bastion.

สามเสาของ PopNx MCP

สามเรื่องนี้ไม่รวมเป็นคะแนนเดียว เพราะความเห็น การวัด และความนิยม เป็นคนละเรื่องกัน

ใช้ทำอะไรได้

ตัวนี้ยังไม่ได้เรียบเรียงเป็นภาษาไทย ด้านล่างคือข้อเท็จจริงที่เราอ่านจาก registry

  • การเชื่อมต่อ: รันบนเครื่อง
  • ช่องทาง (transport): stdio
  • หมวด: ฐานข้อมูลและชุดข้อมูล
  • ต้องใส่ค่าเหล่านี้: DATABASE_URL, PG_PASSWORD, PG_SSH_PASSPHRASE, PG_SSH_PASSWORD

ติดตั้ง

เลือกโปรแกรม AI ที่คุณใช้ แล้วคัดลอกค่าตั้งไปวาง

ก่อนติดตั้ง

  • ผลตรวจเบื้องต้น: ผ่าน 1 จาก 1 ข้อ
  • รันในเครื่องคุณด้วยสิทธิ์ของผู้ใช้คุณ (ไฟล์ อินเทอร์เน็ต แอปต่าง ๆ)
  • ค่าที่คุณต้องใส่เอง: DATABASE_URL, PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DATABASE, PG_ALLOW_WRITE, PG_SSLMODE, PG_SSL_CA, PG_ENABLE_RUNTIME_CONNECT, PG_MAX_RESULT_BYTES, PG_STATEMENT_TIMEOUT, PG_CONNECT_TIMEOUT, PG_SSH_HOST, PG_SSH_PORT, PG_SSH_USER, PG_SSH_PRIVATE_KEY, PG_SSH_PASSPHRASE, PG_SSH_AGENT, PG_SSH_PASSWORD, PG_SSH_FINGERPRINT, PG_SSH_KEEPALIVE_INTERVAL

ค่าที่ติดป้ายความลับคือกุญแจของบัญชีคุณ อย่าส่งให้ใคร และอย่าวางในแชต

ต้องเตรียมก่อน

  • npmติดตั้ง Node.js (มีคำสั่ง npx) ในเครื่อง

คุณใช้แอปไหน

เลือกแอปด้านบน เราจำไว้ให้ ครั้งหน้าปุ่มจะขึ้นว่า “ติดตั้งลง …”

เปิด Claude Desktop → Settings → Developer → Edit Config แล้ววางค่านี้ในไฟล์ claude_desktop_config.json

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres-server"
      ],
      "env": {
        "DATABASE_URL": "ใส่ค่าของคุณ",
        "PG_HOST": "ใส่ค่าของคุณ",
        "PG_PORT": "ใส่ค่าของคุณ",
        "PG_USER": "ใส่ค่าของคุณ",
        "PG_PASSWORD": "ใส่ค่าของคุณ",
        "PG_DATABASE": "ใส่ค่าของคุณ",
        "PG_ALLOW_WRITE": "ใส่ค่าของคุณ",
        "PG_SSLMODE": "ใส่ค่าของคุณ",
        "PG_SSL_CA": "ใส่ค่าของคุณ",
        "PG_ENABLE_RUNTIME_CONNECT": "ใส่ค่าของคุณ",
        "PG_MAX_RESULT_BYTES": "ใส่ค่าของคุณ",
        "PG_STATEMENT_TIMEOUT": "ใส่ค่าของคุณ",
        "PG_CONNECT_TIMEOUT": "ใส่ค่าของคุณ",
        "PG_SSH_HOST": "ใส่ค่าของคุณ",
        "PG_SSH_PORT": "ใส่ค่าของคุณ",
        "PG_SSH_USER": "ใส่ค่าของคุณ",
        "PG_SSH_PRIVATE_KEY": "ใส่ค่าของคุณ",
        "PG_SSH_PASSPHRASE": "ใส่ค่าของคุณ",
        "PG_SSH_AGENT": "ใส่ค่าของคุณ",
        "PG_SSH_PASSWORD": "ใส่ค่าของคุณ",
        "PG_SSH_FINGERPRINT": "ใส่ค่าของคุณ",
        "PG_SSH_KEEPALIVE_INTERVAL": "ใส่ค่าของคุณ"
      }
    }
  }
}

พิมพ์คำสั่งนี้ใน Terminal

claude mcp add postgres --env DATABASE_URL='ใส่ค่าของคุณ' --env PG_HOST='ใส่ค่าของคุณ' --env PG_PORT='ใส่ค่าของคุณ' --env PG_USER='ใส่ค่าของคุณ' --env PG_PASSWORD='ใส่ค่าของคุณ' --env PG_DATABASE='ใส่ค่าของคุณ' --env PG_ALLOW_WRITE='ใส่ค่าของคุณ' --env PG_SSLMODE='ใส่ค่าของคุณ' --env PG_SSL_CA='ใส่ค่าของคุณ' --env PG_ENABLE_RUNTIME_CONNECT='ใส่ค่าของคุณ' --env PG_MAX_RESULT_BYTES='ใส่ค่าของคุณ' --env PG_STATEMENT_TIMEOUT='ใส่ค่าของคุณ' --env PG_CONNECT_TIMEOUT='ใส่ค่าของคุณ' --env PG_SSH_HOST='ใส่ค่าของคุณ' --env PG_SSH_PORT='ใส่ค่าของคุณ' --env PG_SSH_USER='ใส่ค่าของคุณ' --env PG_SSH_PRIVATE_KEY='ใส่ค่าของคุณ' --env PG_SSH_PASSPHRASE='ใส่ค่าของคุณ' --env PG_SSH_AGENT='ใส่ค่าของคุณ' --env PG_SSH_PASSWORD='ใส่ค่าของคุณ' --env PG_SSH_FINGERPRINT='ใส่ค่าของคุณ' --env PG_SSH_KEEPALIVE_INTERVAL='ใส่ค่าของคุณ' -- npx -y mcp-postgres-server

เติมค่าที่เว้นว่างไว้ก่อนรัน

เปิดใน Cursor

หรือคัดลอกค่าตั้งเอง

วางในไฟล์ .cursor/mcp.json ของโปรเจกต์ หรือ ~/.cursor/mcp.json

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres-server"
      ],
      "env": {
        "DATABASE_URL": "ใส่ค่าของคุณ",
        "PG_HOST": "ใส่ค่าของคุณ",
        "PG_PORT": "ใส่ค่าของคุณ",
        "PG_USER": "ใส่ค่าของคุณ",
        "PG_PASSWORD": "ใส่ค่าของคุณ",
        "PG_DATABASE": "ใส่ค่าของคุณ",
        "PG_ALLOW_WRITE": "ใส่ค่าของคุณ",
        "PG_SSLMODE": "ใส่ค่าของคุณ",
        "PG_SSL_CA": "ใส่ค่าของคุณ",
        "PG_ENABLE_RUNTIME_CONNECT": "ใส่ค่าของคุณ",
        "PG_MAX_RESULT_BYTES": "ใส่ค่าของคุณ",
        "PG_STATEMENT_TIMEOUT": "ใส่ค่าของคุณ",
        "PG_CONNECT_TIMEOUT": "ใส่ค่าของคุณ",
        "PG_SSH_HOST": "ใส่ค่าของคุณ",
        "PG_SSH_PORT": "ใส่ค่าของคุณ",
        "PG_SSH_USER": "ใส่ค่าของคุณ",
        "PG_SSH_PRIVATE_KEY": "ใส่ค่าของคุณ",
        "PG_SSH_PASSPHRASE": "ใส่ค่าของคุณ",
        "PG_SSH_AGENT": "ใส่ค่าของคุณ",
        "PG_SSH_PASSWORD": "ใส่ค่าของคุณ",
        "PG_SSH_FINGERPRINT": "ใส่ค่าของคุณ",
        "PG_SSH_KEEPALIVE_INTERVAL": "ใส่ค่าของคุณ"
      }
    }
  }
}

เบราว์เซอร์จะถามก่อนเปิดแอป จากนั้นแอปแสดงค่าตั้งให้ตรวจแล้วกดยืนยัน ช่อง key เว้นว่างไว้ให้คุณใส่เอง

เปิดใน VS Code

หรือคัดลอกค่าตั้งเอง

วางในไฟล์ .vscode/mcp.json ของโปรเจกต์

{
  "servers": {
    "postgres": {
      "type": "stdio",
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres-server"
      ],
      "env": {
        "DATABASE_URL": "ใส่ค่าของคุณ",
        "PG_HOST": "ใส่ค่าของคุณ",
        "PG_PORT": "ใส่ค่าของคุณ",
        "PG_USER": "ใส่ค่าของคุณ",
        "PG_PASSWORD": "ใส่ค่าของคุณ",
        "PG_DATABASE": "ใส่ค่าของคุณ",
        "PG_ALLOW_WRITE": "ใส่ค่าของคุณ",
        "PG_SSLMODE": "ใส่ค่าของคุณ",
        "PG_SSL_CA": "ใส่ค่าของคุณ",
        "PG_ENABLE_RUNTIME_CONNECT": "ใส่ค่าของคุณ",
        "PG_MAX_RESULT_BYTES": "ใส่ค่าของคุณ",
        "PG_STATEMENT_TIMEOUT": "ใส่ค่าของคุณ",
        "PG_CONNECT_TIMEOUT": "ใส่ค่าของคุณ",
        "PG_SSH_HOST": "ใส่ค่าของคุณ",
        "PG_SSH_PORT": "ใส่ค่าของคุณ",
        "PG_SSH_USER": "ใส่ค่าของคุณ",
        "PG_SSH_PRIVATE_KEY": "ใส่ค่าของคุณ",
        "PG_SSH_PASSPHRASE": "ใส่ค่าของคุณ",
        "PG_SSH_AGENT": "ใส่ค่าของคุณ",
        "PG_SSH_PASSWORD": "ใส่ค่าของคุณ",
        "PG_SSH_FINGERPRINT": "ใส่ค่าของคุณ",
        "PG_SSH_KEEPALIVE_INTERVAL": "ใส่ค่าของคุณ"
      }
    }
  }
}

เบราว์เซอร์จะถามก่อนเปิดแอป จากนั้นแอปแสดงค่าตั้งให้ตรวจแล้วกดยืนยัน ช่อง key เว้นว่างไว้ให้คุณใส่เอง

ตัวนี้รันในเครื่องคุณ แต่ ChatGPT ต่อได้เฉพาะ server บนอินเทอร์เน็ต ลองเลือกแอปอื่น

ตัวเลือกเก็บในคุกกี้บนเครื่องนี้ ไม่ต้องมีบัญชี

ค่าที่ต้องใส่เอง

  • DATABASE_URL ความลับ env
    Full connection string (preferred). Supports sslmode in the URL.
  • PG_HOST env
    Database host (fallback when DATABASE_URL is not set).
  • PG_PORT env
    Database port.
  • PG_USER env
    Database user.
  • PG_PASSWORD ความลับ env
    Database password.
  • PG_DATABASE env
    Database name.
  • PG_ALLOW_WRITE env
    When true, execute performs writes and reads are sent directly. Off (default) is read-only: execute refuses writes and each read runs in a READ ONLY transaction.
  • PG_SSLMODE env
    TLS mode. require/allow/prefer encrypt without verifying the certificate; verify-ca/verify-full verify it (supply a CA via PG_SSL_CA). Unlike libpq, allow/prefer do not fall back to plaintext, so a s…
  • PG_SSL_CA env
    Path to a CA certificate file. Setting it by itself implies verify-full.
  • PG_ENABLE_RUNTIME_CONNECT env
    Register the connect_db tool (runtime credential switching).
  • PG_MAX_RESULT_BYTES env
    Byte budget for a query result sent to the model. Whole rows are kept while they fit; over the budget returnedRows < rowCount and truncated is true.
  • PG_STATEMENT_TIMEOUT env
    Statement timeout in milliseconds, applied to every session.
  • PG_CONNECT_TIMEOUT env
    Timeout in milliseconds for a single connect attempt (raise it for slow links or SSH tunnels).
  • PG_SSH_HOST env
    SSH bastion host. Setting it enables tunneling: the server reaches the database only through an SSH tunnel to this host. Needs the ssh2 optional dependency.
  • PG_SSH_PORT env
    SSH bastion port.
  • PG_SSH_USER env
    SSH username.
  • PG_SSH_PRIVATE_KEY env
    Path to a private key file. If unset, auth falls back like ssh: a running agent (SSH_AUTH_SOCK), then a default key (~/.ssh/id_ed25519, id_rsa, id_ecdsa).
  • PG_SSH_PASSPHRASE ความลับ env
    Passphrase for the private key, if encrypted.
  • PG_SSH_AGENT env
    true to use the ambient agent (SSH_AUTH_SOCK), or an explicit socket path / Windows named pipe.
  • PG_SSH_PASSWORD ความลับ env
    SSH login password. Opt-in; a key or agent takes precedence. Prefer keys, a bastion often disables password auth.
  • PG_SSH_FINGERPRINT env
    Pinned host-key fingerprint (SHA256:...). Host-key verification is mandatory and set only this way: without it the tunnel refuses to connect. Get it with ssh-keygen -lF host.
  • PG_SSH_KEEPALIVE_INTERVAL env
    SSH keepalive interval in ms; the tunnel drops after 3 unanswered keepalives, and the next call reconnects.

ค่าตั้งนี้สร้างจากข้อมูลใน registry ของผู้ทำ ถ้าไม่ตรงกับ README ให้เชื่อ README

ทำอะไรได้บ้าง

รันในเครื่องคุณ รายการเครื่องมือจะขึ้นในแอป AI หลังติดตั้ง

ผลตรวจเบื้องต้นi

เราตรวจจากข้อมูลที่ใครก็เปิดดูได้ ยังไม่ได้อ่านโค้ด ข้อที่ไม่เกี่ยวกับตัวนี้ไม่นับในตัวหาร · ตรวจเมื่อ 7 พ.ย. 2569

  • ✓ ผ่าน

    ตัวตนผู้ทำตรงกับ repo

    ชื่อใน registry ยืนยันด้วยบัญชี GitHub antonorlov ซึ่งเป็นเจ้าของ repo

  • … ยังไม่ตรวจ

    ยังมีคนดูแล

    ยังไม่ได้อ่านข้อมูล repo จาก GitHub

  • … ยังไม่ตรวจ

    มีสัญญาอนุญาต (license)

    ยังไม่ได้อ่านข้อมูล repo จาก GitHub

  • – ไม่เกี่ยว

    endpoint ตอบ

    ตัวนี้รันบนเครื่องคุณ ไม่มี endpoint บนอินเทอร์เน็ต

  • – ไม่เกี่ยว

    ล็อกอินแบบ OAuth

    ตัวนี้รันบนเครื่อง ใช้ค่าที่ตั้งในเครื่องแทน

  • … ยังไม่ตรวจ

    คำอธิบายเครื่องมือไม่มีคำสั่งแอบแฝง

    รันในเครื่องคุณ เราอ่านรายการเครื่องมือได้เฉพาะตัวที่มี endpoint บนอินเทอร์เน็ต

เทียบกับตัวอื่นในหมวด ฐานข้อมูลและชุดข้อมูลi

  • ดาวมากกว่า 0% ของอีก 954 ตัวในหมวดนี้
  • ส่งรุ่นใหม่เข้า registry ล่าสุดกว่า 38% ของอีก 954 ตัวในหมวดนี้
  • ผลตรวจเบื้องต้นดีกว่าหรือเท่ากับ 100% ของอีก 954 ตัวในหมวดนี้

เทียบกับทุก server ที่ยังใช้งานในหมวดนี้ทั้งทะเบียน ดาวนับเฉพาะเมื่อผู้ลงทะเบียนเป็นเจ้าของ repo

คำถามที่พบบ่อย

PostgreSQL ปลอดภัยไหม?

จากการตรวจเบื้องต้นของเรา ผ่าน 1 จาก 1 ข้อที่ตรวจได้ เรายังไม่ได้อ่านโค้ด ก่อนใส่กุญแจหรือ token ให้ดูว่า server ขอสิทธิ์อะไร และเลือกกุญแจที่ทำได้เท่าที่จำเป็น

ต้องใช้กุญแจหรือรหัสอะไรบ้าง?

ต้องใส่ค่าเหล่านี้: DATABASE_URL, PG_PASSWORD, PG_SSH_PASSPHRASE, PG_SSH_PASSWORD

ตัวนี้รันบนเครื่องหรือบนคลาวด์?

รันบนเครื่องของคุณ โปรแกรม AI จะเปิด server ขึ้นมาเองเมื่อใช้ ข้อมูลไม่ต้องผ่านเซิร์ฟเวอร์ของผู้ทำ (ยกเว้นตัวที่ไปเรียกบริการออนไลน์ต่อ)

ยังมีคนดูแลอยู่ไหม?

repo บน GitHub มีการแก้โค้ดล่าสุด – และส่งรุ่นล่าสุด (0.3.1) เข้า registry 2 เดือนที่ผ่านมา

ใช้กับโปรแกรมไหนได้บ้าง?

โปรแกรมที่รองรับ MCP เช่น Claude Desktop, Claude Code, Cursor และ VS Code ตัวนี้ต้องมี Node.js ในเครื่องด้วย

มีตัวไหนใช้แทนได้บ้าง?

ในหมวดเดียวกันมี Supabase, MongoDB MCP Server, Elasticsearch MCP Server

ตัวที่ใช้แทนได้

server หมวดเดียวกันจากผู้ทำคนอื่น เรียงตามดาว

อ่านการ์ด:★ ดาว GitHubiสามเสา: รีวิว · ผลตรวจ · การใช้งานiอัปเดต = วันที่ส่งรุ่นล่าสุดเข้า registryi

Supabase

โดย supabase

server ทางการของ Supabase ให้ AI สร้างตาราง ถามข้อมูล และดูการตั้งค่าโปรเจกต์ในฐานข้อมูล Supabase ของคุณ

บนเครื่องหรือคลาวด์★ 2,993Apache-2.0อัปเดต วันนี้
ติดตั้ง

MongoDB MCP Server

โดย mongodb-js

MongoDB Model Context Protocol Server

รันบนเครื่อง★ 1,157Apache-2.0อัปเดต เมื่อวานซืน
ติดตั้ง

Elasticsearch MCP Server

โดย cr7258

MCP server for interacting with Elasticsearch

รันบนเครื่อง★ 315Apache-2.0อัปเดต 14 เดือนที่ผ่านมา
ติดตั้ง

README จากผู้ทำ

เปิดอ่าน README

ส่วนนี้คัดจาก repo ของผู้ทำตามต้นฉบับ PopNx MCP ไม่ได้ตรวจหรือรับรองเนื้อหา ตัด HTML รูปภาพ และสคริปต์ออกแล้ว ลิงก์จะพาออกนอกเว็บ

MCP PostgreSQL Server

A Model Context Protocol (MCP) server for PostgreSQL: local, Docker, RDS, Neon, and Supabase databases.

The server is small and auditable, with four runtime dependencies: the MCP SDK, pg, pg-connection-string, and zod (plus ssh2, an optional dependency used only for SSH tunneling).

Requires Node.js 20 or newer.

Quick start

The preferred way to configure the server is a single DATABASE_URL:

{

"mcpServers": { "postgres": { "type": "stdio", "command": "npx", "args": ["-y", "mcp-postgres-server"], "env": { "DATABASE_URL": "postgres://user:password@localhost:5432/mydb", "PG_ALLOW_WRITE": "false" } } } }

With PG_ALLOW_WRITE set to "false" the server has read-only access to the database. This is the default; set it to "true" only if the model must write.

The same JSON works in any MCP client that speaks stdio: VS Code, Cursor, Claude Code, Codex, Windsurf.

Alternatively, set the individual PG_* variables; they are used when DATABASE_URL is not set:

{

"mcpServers": { "postgres": { "type": "stdio", "command": "npx", "args": ["-y", "mcp-postgres-server"], "env": { "PG_HOST": "your_host", "PG_PORT": "5432", "PG_USER": "your_user", "PG_PASSWORD": "your_password", "PG_DATABASE": "your_database", "PG_ALLOW_WRITE": "false" } } } }

Manual Installation
npm install mcp-postgres-server

Or run directly with:

npx mcp-postgres-server

Connect to your database

Local Postgres:

DATABASE_URL=postgres://mcp_readonly:secret@localhost:5432/mydb

Postgres in Docker: if the database runs in a container with a published port, connect to localhost: as usual. If the *MCP server itself* runs inside a container and the database runs on your host machine, use host.docker.internal instead of localhost:

DATABASE_URL=postgres://mcp_readonly:secret@host.docker.internal:5432/mydb

Amazon RDS:

DATABASE_URL=postgres://mcp_readonly:secret@mydb.xxxxxx.us-east-1.rds.amazonaws.com:5432/mydb?sslmode=require

Neon:

DATABASE_URL=postgres://mcp_readonly:secret@ep-xxx-xxx.us-east-2.aws.neon.tech/mydb?sslmode=require

Supabase:

DATABASE_URL=postgres://postgres.xxxxxxxx:secret@aws-0-us-east-1.pooler.supabase.com:5432/postgres?sslmode=require

Tools

Tool availability depends on configuration:

ToolAvailable
query, list_schemas, list_tables, describe_tableAlways
executeAlways (refuses writes unless PG_ALLOW_WRITE=true)
connect_dbOnly when PG_ENABLE_RUNTIME_CONNECT=true
1. query

Execute a read-only SQL statement. Accepts SELECT, WITH ... SELECT, EXPLAIN, and SHOW. One statement per call - multi-statement input is rejected by the extended query protocol. In read-only mode (the default) the statement runs as BEGIN READ ONLY, the query, and ROLLBACK - three commands, roughly two network round trips with pipelining - so the database itself refuses any write. With PG_ALLOW_WRITE=true the statement is sent directly, without that wrapper, so a write run through query would execute - use execute for writes. Supports PostgreSQL-style $1, $2 prepared-statement parameters; values are bound by the driver and never spliced into the SQL text.

use_mcp_tool({

server_name: "postgres", tool_name: "query", arguments: { sql: "SELECT * FROM users WHERE id = $1", params: [1] } });

Returns compact JSON: {"rows": [...], "rowCount": n, "returnedRows": n, "truncated": false}. When the serialized rows exceed PG_MAX_RESULT_BYTES, only the rows that fit are returned (returnedRows < rowCount), truncated is true, and a hint suggests adding LIMIT/WHERE or selecting fewer columns.

2. list_schemas

List all schemas in the connected database.

use_mcp_tool({

server_name: "postgres", tool_name: "list_schemas", arguments: {} });

3. list_tables

List tables in the connected database. Accepts an optional schema parameter (defaults to 'public').

// List tables in the 'public' schema (default)

use_mcp_tool({ server_name: "postgres", tool_name: "list_tables", arguments: {} });

// List tables in a specific schema use_mcp_tool({ server_name: "postgres", tool_name: "list_tables", arguments: { schema: "my_schema" } });

4. describe_table

Get the structure of a specific table (columns, types, nullability, defaults, primary keys). Accepts an optional schema parameter (defaults to 'public').

use_mcp_tool({

server_name: "postgres", tool_name: "describe_table", arguments: { table: "users", schema: "my_schema" // optional } });

5. execute - requires PG_ALLOW_WRITE=true

Execute an INSERT, UPDATE, DELETE, or DDL statement. Always registered, but in read-only mode (the default) it refuses with an error naming PG_ALLOW_WRITE and changes nothing - the statement never reaches the database. With PG_ALLOW_WRITE=true it runs: same $1, $2 parameter handling as query, one complete statement per call, and the connecting role governs what it may do. Returns {"rowCount": n, "command": "INSERT"}.

use_mcp_tool({

server_name: "postgres", tool_name: "execute", arguments: { sql: "INSERT INTO users (name, email) VALUES ($1, $2)", params: ["John Doe", "john@example.com"] } });

6. connect_db - requires PG_ENABLE_RUNTIME_CONNECT=true

Connect to a different PostgreSQL database at runtime using provided credentials. Not registered by default - prefer configuring credentials through the environment so they never pass through model-visible arguments. Session limits (statement_timeout, idle_in_transaction_session_timeout) are re-applied after every reconnect; read-only reads enforce read-only in their own BEGIN READ ONLY transaction.

use_mcp_tool({

server_name: "postgres", tool_name: "connect_db", arguments: { host: "localhost", port: 5432, user: "your_user", password: "your_password", database: "your_database" } });

Configuration reference

VariableDefaultDescription
DATABASE_URL-Full connection string (preferred). Supports ?sslmode= in the URL.
PG_HOST-Database host (fallback when DATABASE_URL is not set)
PG_PORT5432Database port
PG_USER-Database user
PG_PASSWORD-Database password
PG_DATABASE-Database name
PG_ALLOW_WRITEfalseWhen true, execute performs writes and reads are sent directly. Off (default) is read-only: execute refuses writes and each read runs in a READ ONLY transaction
PG_SSLMODE-disable \allow \prefer \require \verify-ca \verify-full. require/allow/prefer encrypt without verifying the certificate; verify-ca/verify-full verify (supply a CA via PG_SSL_CA). Unrecognized values fail at startup. Limitation: unlike libpq, allow/prefer do not fall back to plaintext (node-postgres has no opportunistic SSL), so a server without TLS needs disable.
PG_SSL_CA-Path to a CA certificate file. Setting it by itself implies verify-full
PG_ENABLE_RUNTIME_CONNECTfalseRegister the connect_db tool (runtime credential switching)
PG_MAX_RESULT_BYTES32768Byte budget for a query result sent to the model. Whole rows are kept while they fit; over the budget returnedRows < rowCount and truncated: true (if not even the first row fits, returnedRows is 0 with a hint). ~32 KiB ≈ 8k tokens; lower it for strict clients, raise it if your client allows more.
PG_STATEMENT_TIMEOUT30000Statement timeout in milliseconds, applied to every session
PG_CONNECT_TIMEOUT10000Timeout in milliseconds for a single connect attempt (raise it for slow links or SSH tunnels)

To reach a database only accessible through a bastion, see SSH tunneling (adds PG_SSH_* variables).

Features

  • Read-only by default; writes are an explicit opt-in (PG_ALLOW_WRITE=true)
  • Read-only enforced by the engine (BEGIN READ ONLY), never by client-side SQL parsing
  • Data access behind a small typed interface; the pg driver never leaks past it
  • DATABASE_URL support with SSL (sslmode=disable|allow|prefer|require|verify-ca|verify-full, custom CA)
  • Prepared-statement parameters: $1-style placeholders, bound by the driver
  • Result size cap (byte budget) with an explicit truncated flag instead of flooding the model's context
  • Session statement timeout plus a client deadline; transaction poolers may not preserve session settings
  • Errors returned as readable tool results with SQLSTATE-based hints, so the model can self-correct
  • Survives dropped connections - reconnects lazily instead of crashing
  • Optional SSH tunneling (PG_SSH_*) with mandatory host-key verification, loaded only when configured
  • MCP tool annotations (read-only / destructive hints) per spec 2025-11-25
  • Multi-schema support for database operations

Security

Full details, including the threat model and disclosure process, are in SECURITY.md. The short version:

  1. A least-privilege database role is the real boundary. The MCP works with existing credentials; creating or changing roles is not required. A dedicated role is what actually guarantees writes are impossible. On PostgreSQL 14+, the following is a starting point:
   CREATE ROLE mcp_readonly LOGIN PASSWORD 'change-me';

GRANT CONNECT ON DATABASE your_database TO mcp_readonly; GRANT pg_read_all_data TO mcp_readonly; -- adds read privileges ALTER ROLE mcp_readonly SET default_transaction_read_only = on; -- read-only by default

(On PostgreSQL 13 or older, grant SELECT explicitly instead of pg_read_all_data - see SECURITY.md.) The server warns on stderr if you connect as a superuser. Read grants do not revoke existing privileges, and defaults remain mutable; available functions, ownership and inherited privileges also matter.

  1. The engine enforces read-only. There is no client-side SQL parsing. In read-only mode every read runs in a rolled-back BEGIN READ ONLY transaction, so PostgreSQL itself - which alone knows what a function, view, or rule does - refuses any write with SQLSTATE 25006 and reverts any session change the statement made. The extended protocol rejects multi-command strings.

Honest framing: the read-only transaction is defense-in-depth on top of the role, not a replacement for it. Read-only mode stops a confused or prompt-injected model from *writing* to your database; it does not stop prompt injection carried in the row data a query returns. Don't point this server at production - use a replica, a snapshot, or a tightly scoped role. See SECURITY.md.

SSH tunneling

Set PG_SSH_HOST (plus auth and host-key verification) to reach a database that is only accessible through a bastion (an SSH jump host). The connection string / PG_* fields then describe the database as seen from the bastion:

{

"mcpServers": { "postgres": { "type": "stdio", "command": "npx", "args": ["-y", "mcp-postgres-server"], "env": { "DATABASE_URL": "postgres://mcp_readonly:secret@db.internal:5432/mydb?sslmode=verify-full",

ตัดมาแค่ช่วงต้น อ่านต่อใน repo

ถาม PopNx