# PostgreSQL

Connects AI assistants to PostgreSQL instances locally or in cloud providers like Neon and RDS. Inspect table schemas and run read-only queries from chat.

- MCP server · `type=mcp` · Claude Code, Codex, Cursor, GitHub Copilot, Windsurf (Devin Desktop), Cline, Zed, Gemini CLI · ★ 24
- Author: antonorlov
- License: MIT · policy=link-only · description only; the file stays with the author
- Source: https://github.com/antonorlov/mcp-postgres-server
- Updated: 2026-09-16
- HTML: https://affpapa.org/en/nevibecode/mcp/postgresql

## What it does

The server runs via `npx` over stdio and needs Node.js 20 or newer. It keeps dependencies minimal, relying on `pg`, `zod`, and `pg-connection-string`. A single `DATABASE_URL` is enough for setup, though discrete host and port variables are also supported.

Safety is the default. In read-only mode, queries run inside a `BEGIN READ ONLY` block followed by `ROLLBACK`, guaranteeing that writes cannot slip through. The agent inspects schemas and fetches column details with tools like `describe_table` and `list_tables`.

Write operations stay blocked. Unless `PG_ALLOW_WRITE` is explicitly set to `true`, the `execute` tool refuses to run any mutating statements.

## Requirements
- Needs: Node.js (commands run via npx)
- Environment variables: `DATABASE_URL`, `PG_HOST`, `PG_PORT`, `PG_USER`, `PG_PASSWORD`, `PG_DATABASE`, `PG_ALLOW_WRITE`, `PG_SSLMODE`, `PG_SSL_CA`, `PG_ENABLE_RUNTIME_CONNECT` — keys and tokens: ask the human for the values, never commit them or put them in shared files.
- Works with: PostgreSQL

## Install

### Claude Code (native platform)

**Platform CLI**
```bash
claude mcp add --env DATABASE_URL=<DATABASE_URL> --env PG_HOST=<PG_HOST> --env PG_PORT=<PG_PORT> --env PG_USER=<PG_USER> --env PG_PASSWORD=<PG_PASSWORD> --transport stdio postgresql -- npx -y mcp-postgres-server
```
Replace <…> with your own values. 17 more environment variables are in the server README

### Codex

**Platform CLI**
```bash
codex mcp add postgresql --env DATABASE_URL=<DATABASE_URL> --env PG_HOST=<PG_HOST> --env PG_PORT=<PG_PORT> --env PG_USER=<PG_USER> --env PG_PASSWORD=<PG_PASSWORD> -- npx -y mcp-postgres-server
```
Replace <…> with your own values. 17 more environment variables are in the server README

### Cursor

**Config file**
File `.cursor/mcp.json`:
```json
{
    "mcpServers": {
        "postgresql": {
            "command": "npx",
            "args": [
                "-y",
                "mcp-postgres-server"
            ],
            "env": {
                "DATABASE_URL": "<DATABASE_URL>",
                "PG_HOST": "<PG_HOST>",
                "PG_PORT": "<PG_PORT>",
                "PG_USER": "<PG_USER>",
                "PG_PASSWORD": "<PG_PASSWORD>"
            }
        }
    }
}
```
Replace <…> with your own values. 17 more environment variables are in the server README

### GitHub Copilot

**Config file**
File `.vscode/mcp.json`:
```json
{
    "servers": {
        "postgresql": {
            "type": "stdio",
            "command": "npx",
            "args": [
                "-y",
                "mcp-postgres-server"
            ],
            "env": {
                "DATABASE_URL": "<DATABASE_URL>",
                "PG_HOST": "<PG_HOST>",
                "PG_PORT": "<PG_PORT>",
                "PG_USER": "<PG_USER>",
                "PG_PASSWORD": "<PG_PASSWORD>"
            }
        }
    }
}
```
Replace <…> with your own values. 17 more environment variables are in the server README

### Gemini CLI

**Platform CLI**
```bash
gemini mcp add -e DATABASE_URL=<DATABASE_URL> -e PG_HOST=<PG_HOST> -e PG_PORT=<PG_PORT> -e PG_USER=<PG_USER> -e PG_PASSWORD=<PG_PASSWORD> postgresql npx -y mcp-postgres-server
```
Replace <…> with your own values. 17 more environment variables are in the server README

## When to use it
- inspect database tables and column types
- run read-only SQL queries with bound parameters
- connect your coding agent to Neon or Docker databases
- safely explore production schemas without accidental writes

## Example prompts
- List all available tables in the `public` schema.
- Describe the structure and columns of the `users` table.
- Run `SELECT id, name FROM users WHERE email = $1` with parameter `test@example.com`.

## Similar
- [World Monitor](https://affpapa.org/en/nevibecode/mcp/world-monitor.md) — MCP server · ★ 89408
- [Scrapling MCP Server](https://affpapa.org/en/nevibecode/mcp/scrapling-mcp-server.md) — MCP server · ★ 89433
- [MCP Toolbox for Databases](https://affpapa.org/en/nevibecode/mcp/googleapis-mcp-toolbox-for-databases.md) — MCP server · ★ 16717
- [PostHog MCP Server](https://affpapa.org/en/nevibecode/mcp/posthog-mcp-server.md) — MCP server · ★ 40409
- [dbx](https://affpapa.org/en/nevibecode/mcp/dbx.md) — MCP server · ★ 27669
- [Metabase](https://affpapa.org/en/nevibecode/mcp/metabase.md) — MCP server · ★ 49845

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
