# pg-mcp-server — Postgres MCP Server (Read-Only) > Connect LLMs to PostgreSQL via MCP with pg-mcp-server: run via npx, default stdio transport, and keep writes disabled unless explicitly enabled. ## Install Merge the JSON below into your `.mcp.json`: ## Quick Use ```bash export DATABASE_URL='postgresql://postgres:postgres@localhost:5432/postgres' npx --yes pg-mcp-server --transport stdio # Optional HTTP: npx --yes pg-mcp-server --transport http (serves /mcp on port 3000) ``` ## Intro Connect LLMs to PostgreSQL via MCP with pg-mcp-server: run via npx, default stdio transport, and keep writes disabled unless explicitly enabled. **Best for:** Developers who want controlled Postgres access for agents (read-only by default) **Works with:** Any MCP client that can run stdio or streamable HTTP; requires DATABASE_URL **Setup time:** 6-15 minutes ### Key facts (verified) - GitHub: 176 stars · 15 forks · pushed 2025-11-03. - License: MIT · owner avatar + repo URL verified via GitHub API. - README-backed entrypoint: `npx --yes pg-mcp-server --transport stdio`. ## Main - Keep writes off: README documents `DANGEROUSLY_ALLOW_WRITE_OPS=false` by default; start with read-only query + schema inspection. - Pick the transport your client supports: stdio is the default; HTTP mode serves a Streamable HTTP endpoint at `/mcp` (PORT defaults to 3000). - Use the provided resources (`postgres://tables`, `postgres://table/{schema}/{table}`) to keep prompts short and structured. ### Source-backed notes - README provides an MCP client config snippet that runs `npx --yes pg-mcp-server --transport stdio` with `DATABASE_URL` set. - README lists env vars: DATABASE_URL, DANGEROUSLY_ALLOW_WRITE_OPS (default false), DEBUG, and PG_SSL_ROOT_CERT. - README documents tools/resources including `query` plus `postgres://tables` and `postgres://table/{schema}/{table}`. ### FAQ - **Can it connect over HTTP?**: Yes. README says HTTP mode serves `/mcp` on PORT (default 3000). - **Is it safe to enable writes?**: Only if you understand the risk. Keep writes disabled until you have guardrails and allowlists. - **Do I need a global install?**: No. README supports running via `npx --yes ...` without installing globally. ## Source & Thanks > Source: https://github.com/ericzakariasson/pg-mcp-server > License: MIT > GitHub stars: 176 · forks: 15 --- ## Quick Use ```bash export DATABASE_URL='postgresql://postgres:postgres@localhost:5432/postgres' npx --yes pg-mcp-server --transport stdio # Optional HTTP: npx --yes pg-mcp-server --transport http (serves /mcp on port 3000) ``` ## Intro 用 pg-mcp-server 把 LLM 接到 PostgreSQL:可用 npx 直接运行,默认 stdio 传输且写操作默认关闭;同时支持 HTTP(/mcp) 模式、query 工具与表结构资源,适合安全的只读分析。 **Best for:** 想把 Postgres 安全接入 agent 的开发者(默认只读) **Works with:** 支持 stdio 或 streamable HTTP 的 MCP 客户端;需要设置 DATABASE_URL **Setup time:** 6-15 minutes ### Key facts (verified) - GitHub:176 stars · 15 forks;最近更新 2025-11-03。 - 许可证:MIT;作者头像与仓库链接均已通过 GitHub API 复核。 - README 中可对照的入口:`npx --yes pg-mcp-server --transport stdio`。 ## Main - 写操作先别开:README 说明默认 `DANGEROUSLY_ALLOW_WRITE_OPS=false`,先以只读 query + schema 检视为主。 - 按客户端能力选择传输:默认 stdio;HTTP 模式会在 `/mcp` 提供 Streamable HTTP 端点(PORT 默认 3000)。 - 善用资源接口降低提示词长度:`postgres://tables` 与 `postgres://table/{schema}/{table}` 更适合结构化上下文。 ### Source-backed notes - README 提供 MCP 客户端配置示例:使用 `npx --yes pg-mcp-server --transport stdio` 并设置 `DATABASE_URL`。 - README 列出环境变量:DATABASE_URL、DANGEROUSLY_ALLOW_WRITE_OPS(默认 false)、DEBUG、PG_SSL_ROOT_CERT。 - README 描述工具/资源:`query` 工具,以及 `postgres://tables`、`postgres://table/{schema}/{table}` 等资源路径。 ### FAQ - **支持 HTTP 连接吗?**:支持。README 说明 HTTP 模式在 PORT(默认 3000)的 `/mcp` 提供端点。 - **开启写操作安全吗?**:需要谨慎。建议在没有明确 guardrails/白名单前保持禁用。 - **必须全局安装吗?**:不必。README 支持 `npx --yes ...` 直接运行。 ## Source & Thanks > Source: https://github.com/ericzakariasson/pg-mcp-server > License: MIT > GitHub stars: 176 · forks: 15 --- Source: https://tokrepo.com/en/workflows/pg-mcp-server-postgres-mcp-server-read-only Author: MCP Hub