# sw-postgres MCP server

> Enables AI agents to safely interact with PostgreSQL databases using a two-phase write process with mandatory human approval for high-impact changes.

- Repository: https://github.com/jpka/sw-postgres-mcp
- Page: https://hostedmcp.org/servers/jpka/sw-postgres-mcp
- Transport: stdio
- Stars: 0
- License: MIT
- Latest version: 0.3.1

## Install

### Claude Code

```sh
claude mcp add --env 'DATABASE_URL_READONLY=<YOUR_DATABASE_URL_READONLY>' --env 'DATABASE_URL_WRITER=<YOUR_DATABASE_URL_WRITER>' sw-postgres-mcp -- npx -y sw-postgres-mcp
```

### Cursor (`~/.cursor/mcp.json`)

```json
{
  "mcpServers": {
    "sw-postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "sw-postgres-mcp"
      ],
      "env": {
        "DATABASE_URL_READONLY": "<YOUR_DATABASE_URL_READONLY>",
        "DATABASE_URL_WRITER": "<YOUR_DATABASE_URL_WRITER>"
      }
    }
  }
}
```

### VS Code (`.vscode/mcp.json`)

```json
{
  "servers": {
    "sw-postgres-mcp": {
      "type": "stdio",
      "command": "npx",
      "args": [
        "-y",
        "sw-postgres-mcp"
      ],
      "env": {
        "DATABASE_URL_READONLY": "<YOUR_DATABASE_URL_READONLY>",
        "DATABASE_URL_WRITER": "<YOUR_DATABASE_URL_WRITER>"
      }
    }
  }
}
```

### VS Code CLI

```sh
code --add-mcp '{"name":"sw-postgres-mcp","type":"stdio","command":"npx","args":["-y","sw-postgres-mcp"],"env":{"DATABASE_URL_READONLY":"<YOUR_DATABASE_URL_READONLY>","DATABASE_URL_WRITER":"<YOUR_DATABASE_URL_WRITER>"}}'
```

### Claude Desktop (`claude_desktop_config.json`)

```json
{
  "mcpServers": {
    "sw-postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "sw-postgres-mcp"
      ],
      "env": {
        "DATABASE_URL_READONLY": "<YOUR_DATABASE_URL_READONLY>",
        "DATABASE_URL_WRITER": "<YOUR_DATABASE_URL_WRITER>"
      }
    }
  }
}
```

### Codex

```sh
codex mcp add sw-postgres-mcp --env 'DATABASE_URL_READONLY=<YOUR_DATABASE_URL_READONLY>' --env 'DATABASE_URL_WRITER=<YOUR_DATABASE_URL_WRITER>' -- npx -y sw-postgres-mcp
```

### Codex config.toml (`~/.codex/config.toml`)

```toml
[mcp_servers.sw-postgres-mcp]
command = "npx"
args = ["-y", "sw-postgres-mcp"]

[mcp_servers.sw-postgres-mcp.env]
DATABASE_URL_READONLY = "<YOUR_DATABASE_URL_READONLY>"
DATABASE_URL_WRITER = "<YOUR_DATABASE_URL_WRITER>"
```

### Gemini CLI

```sh
gemini mcp add -s user -e 'DATABASE_URL_READONLY=<YOUR_DATABASE_URL_READONLY>' -e 'DATABASE_URL_WRITER=<YOUR_DATABASE_URL_WRITER>' sw-postgres-mcp npx -- -y sw-postgres-mcp
```

### Gemini settings.json (`~/.gemini/settings.json`)

```json
{
  "mcpServers": {
    "sw-postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "sw-postgres-mcp"
      ],
      "env": {
        "DATABASE_URL_READONLY": "<YOUR_DATABASE_URL_READONLY>",
        "DATABASE_URL_WRITER": "<YOUR_DATABASE_URL_WRITER>"
      }
    }
  }
}
```

## Environment variables

| Name | Required | Secret | Description |
|---|---|---|---|
| `DATABASE_URL_READONLY` | yes | yes | Postgres connection string for the read-only role. |
| `DATABASE_URL_WRITER` | yes | yes | Postgres connection string for the writer role. |
| `SW_POSTGRES_CONFIG` | no | no |  |

## Tools (8)

- `describe_schema`
- `query`
- `explain_plan`
- `delete_rows`
- `insert_rows`
- `update_rows`
- `run_migration`
- `execute_plan`

## FAQ

### How does the server ensure write safety?

It uses a two-phase write process where every write tool previews a change inside a transaction that always rolls back, requiring a separate execute_plan call with a specific token to commit.

### Can an AI agent approve its own database changes?

No, the approval mechanism is intentionally kept out of reach of the agent; it requires a human to use a local web UI to approve or reject pending plans.

### What happens if a write affects too many rows?

If the affected rows exceed a hard maximum threshold, the request is refused outright without issuing a plan token.

### Does the server prevent SQL injection?

The server uses parameterized queries for structured DML tools like insert_rows and update_rows to mitigate injection risks, though DDL migrations are sent as raw statements.
