# sw-postgres-mcp [Health: Active]

**Category:** 🗄️ Databases  
**Repository:** https://github.com/jpka/sw-postgres-mcp  
**GitHub Stars:** 0  
**Views:** 0  
**Installs:** 0  
**Upvotes:** 0  
**Directory Page:** https://allmcps.com/mcp/sw-postgres-mcp

## Description
Safe-write Postgres MCP server with preview-before-execute writes and rollback safety.

## Tools
Capabilities this server exposes over MCP:

- **describe_schema** — tables, columns with types, foreign keys, row-count estimates (respects read allowlist).
- **query** — run a read-only `SELECT` and return `{ columns, rows, row_count }`. Runs on the readonly role, so a mutating statement is refused by the database regardless of what the SQL says. Enforces a single statement per call and the read allowlist. Optional `limit` and `params`.
- **explain_plan** — run `EXPLAIN (FORMAT JSON)` for a candidate read statement and return the planner's estimated `cost` and `rows` without executing it. A cheap pre-check before running something potentially expensive.
- **delete_rows** — **two-phase delete**. Runs the statement inside a transaction, returns the exact affected row count plus a sample of affected rows, then rolls back. The response includes a `plan_token`, the exact `statement`, and `params` — and a `status` of `previewed` or `awaiting_approval` (see [Approval thresh…
- **insert_rows** — **two-phase insert**. Takes `table`, `columns` (an array of column names), and `rows` (an array of value-arrays, one per row, positional against `columns`), plus `reason`. Runs the `INSERT ... VALUES (...), (...) RETURNING *` inside a transaction, returns the exact row count and a sample of the row…
- **update_rows** — **two-phase update**. Takes `table`, `set` (a `{ "column": value, ... }` object of what to change), `where` + `params` (parameterized WHERE conditions, same convention as `delete_rows`), `confirm_full_table`, and `reason`. Runs the `UPDATE ... SET ... WHERE ... RETURNING *` inside a transaction, re…
- **run_migration** — **two-phase DDL**. Takes `statement` (a single `CREATE TABLE`, `ALTER TABLE`, `DROP TABLE`, or `CREATE [UNIQUE] INDEX ... ON <table>` statement) and `reason`. Runs it inside a transaction — Postgres DDL is transactional, so this rolls back cleanly — then rolls back. **Always requires human approval…
- **execute_plan** — commits a previously previewed write. Pass back the `plan_token`, `statement`, and `params` from the preview response (`delete_rows`, `insert_rows`, `update_rows`, or `run_migration`). If the plan is still awaiting approval, the call **blocks** until a human approves it through the [localhost appro…

## Claude Desktop Quick Installation
No generated config for this server: it installs through its own setup steps.

The project's own setup steps (from https://github.com/jpka/sw-postgres-mcp), copied verbatim. Follow them in order; they are not an mcpServers config.

1. Start the server:

```sh
docker compose up -d
```

2. Install:

```sh
npm install
```

3. Run:

```sh
npm run build
```

4. Start the server:

```sh
node dist/index.js
```

5. Start the server:

```sh
docker compose up
```

6. Start the server:

```sh
docker compose up -d --wait
```

## Documentation

## What sw-postgres-mcp MCP server does

The sw-postgres-mcp MCP server exposes eight MCP tools for inspecting PostgreSQL, analyzing read queries, previewing changes, and committing approved plans. Its central behavior is separating preview from execution: write requests run against the database, report what they would change, and then roll back rather than modifying persistent data.

The read tools are `describe_schema`, `query`, and `explain_plan`. Schema inspection returns tables, column types, foreign keys, and estimated row counts subject to the read allowlist. `query` accepts one read-only `SELECT` statement and returns columns, rows, and a row count. `explain_plan` returns PostgreSQL's JSON planner estimate without running the candidate statement.

Four tools create previews: `delete_rows`, `insert_rows`, `update_rows`, and `run_migration`. The first three report the exact affected-row count and a sample of returned rows. Migration previews cover one supported DDL statement and report its target, but do not return DML-style affected rows. `execute_plan` is the only tool that commits a change.

## How it works

The sw-postgres-mcp MCP server uses separate PostgreSQL connection pools and roles. Reads use a `readonly` role with `SELECT` access, while preview and execution operations use a `writer` role. Database permissions therefore provide a safety boundary in addition to the server's SQL checks.

Each DML preview begins a transaction, runs the real statement with its values parameterized, captures the result, and rolls the transaction back. DDL previews use the same rollback approach; PostgreSQL DDL is transactional. A preview returns a plan token, statement, parameters, and a status such as `previewed` or `awaiting_approval`.

The token is derived from a SHA-256 fingerprint of the statement and parameters. During execution, the server recomputes that fingerprint and refuses changed input. It also rejects execution when the affected row set differs from the one observed during preview. Plans requiring approval are surfaced through a human-only approval interface bound to localhost; approval is not an MCP tool available to the agent.

## Setup and configuration

The repository's quick-start sequence is to start the Docker services, install dependencies, run the test suite, and build the project:

```bash
docker compose up -d
npm install
npm test
npm run build
```

Run the built server with `node dist/index.js`. Startup also launches the approval UI at `http://127.0.0.1:4319/` and prints a per-session token once to stderr. Requests to that interface require the token as a bearer credential or query parameter.

## Tools and capabilities

- `describe_schema`: Lists allowed tables, typed columns, foreign keys, and estimated row counts.
- `query`: Executes one allowlisted, read-only `SELECT` with optional parameters and a result limit.
- `explain_plan`: Gets estimated cost and row count without executing the query.
- `delete_rows`: Previews a parameterized deletion and returns affected-row details.
- `insert_rows`: Previews positional multi-row inserts for a specified table and columns.
- `update_rows`: Previews parameterized updates using a `set` object and `where` conditions.
- `run_migration`: Previews one supported `CREATE TABLE`, `ALTER TABLE`, `DROP TABLE`, or index statement; human approval is always required.
- `execute_plan`: Commits only a matching, previously previewed write.

## Limitations and notes

Read calls are restricted to a single statement and the configured read allowlist. The readonly database role refuses mutating statements even if SQL text checks were bypassassed. DDL supplied to `run_migration` is raw SQL rather than parameterized DML, so its protection comes from transactional preview and mandatory human approval.

A preview is not an unrestricted transaction: it always rolls back until `execute_plan` succeeds. Plans can be refused when their affected-row count exceeds the configured hard maximum, and an approval-required plan cannot be committed until a human approves it through the localhost interface.

_Full upstream README: https://allmcps.com/mcp/sw-postgres-mcp/readme_

