MCP server/database

Postgres Safe MCP Server

Automatically detect and obfuscate PII in PostgreSQL query results

★ 4sam-david/pg-redact-mcp ↗by sam-davidupdated
1

Add it to Claude Code

claude mcp add -e "DATABASE_URL=${DATABASE_URL}" postgres-safe -- npx -y pg-redact-mcp
Required:DATABASE_URL
2

Make your agent remember this setup

postgres-safe's config, env vars, and the gotchas you hit — recalled in every future Claude Code, Cursor, and Codex session.

npx conare@latest

Free · one command · indexes the sessions already on disk. Set up in the browser instead →

What it does

  • Automatic PII detection using column-name heuristics and Microsoft Presidio NLP
  • Masking by default for sensitive data like emails, names, and SSNs
  • Selective unmasking capabilities via AI tool parameters
  • Hard-coded protection for secret columns like passwords and tokens
  • Human-in-the-loop approval for unmasking requests

Tools 3

queryExecutes a SQL query against the database with automatic PII masking applied to the results.
describe_schemaRetrieves the database schema to help the AI understand table and column structures.
configure_maskingUpdates masking rules in-memory for the current session.

Environment Variables

DATABASE_URLrequiredThe connection string for the PostgreSQL database.

Try it

Show me the last 5 registrations from the users table.
How many users signed up last month?
What is the email for registration 5015?
Are there any duplicate registrations in the database?
Original README from sam-david/pg-redact-mcp

postgres-safe-mcp

A PostgreSQL MCP server that automatically detects and obfuscates PII in query results before they reach the AI. Connect Claude (or any MCP client) to your database without exposing sensitive data.

How it works

  1. Connect — Point the server at any PostgreSQL database (local or remote)
  2. Schema check — The agent calls describe_schema first to learn the actual table and column names, avoiding guesswork and failed queries
  3. Auto-detect — On startup, the server runs instant heuristic detection on all column names to classify PII (emails, names, phones, SSNs, etc.). No data sampling required — column name patterns catch the vast majority of PII fields
  4. Mask by default — Query results are automatically masked before the AI sees them:
    PII masked: first_name (MASKED: PERSON), last_name (MASKED: PERSON), email (MASKED: EMAIL_ADDRESS)
    3 rows
    
    | id   | first_name | last_name | email         | created_at |
    |------|------------|-----------|---------------|------------|
    | 5022 | S**        | D****     | s***@g***.com | 2026-03-06 |
    | 5021 | S**        | D****     | s***@g***.com | 2026-03-03 |
    | 5020 | l***       | c***      | l***@t***.com | 2026-02-24 |
    
  5. Reveal when needed — The AI can selectively unmask specific columns or PII types when the user explicitly asks to see real data

Masking and unmasking

Default behavior: everything masked

Every query runs through the redaction engine before results reach the AI. PII columns are detected by name pattern and masked automatically. The AI sees partial values like J****** instead of Jessica — enough structure to reason about the data without exposing real PII.

How the AI decides what to reveal

The query tool accepts two optional parameters for selective unmasking:

  • reveal_columns — Unmask specific columns by name (e.g. ["email", "first_name"])
  • reveal_types — Unmask all columns of a PII type (e.g. ["EMAIL_ADDRESS"])

The AI is guided by these rules in the tool description:

Scenario What the AI does
"Show me the last 5 registrations" Keeps everything masked — browsing doesn't need real data
"How many users signed up last month?" Aggregate query, no PII in results
"What is the email for registration 5015?" Reveals email — user explicitly asked for it
"Show me John's full name" Reveals first_name, last_name — user asked to see the value
"Are there duplicate registrations?" Keeps masked — duplicates are detectable from masked patterns

Secret columns can never be revealed

Columns matching patterns like encrypted_password, otp_secret_key, reset_password_token are classified as SECRET. These are always fully redacted ([REDACTED]) and cannot be unmasked even if reveal_columns or reveal_types is used. There's no legitimate reason for an AI agent to see raw password hashes or auth tokens.

Manual overrides

You can force specific masking behavior per column in your config:

column_rules:
  # Force a column to be treated as PII even if the name doesn't match patterns
  - table: users
    column: custom_id_field
    pii_type: US_SSN
    masking_style: partial

  # Explicitly mark a column as NOT PII (skip masking)
  - table: users
    column: display_name    # public-facing, not sensitive
    pii_type: none
    masking_style: none

Or at runtime via the configure_masking tool (in-memory, not persisted).

Human in the loop

In Claude Code, every tool call is shown to the user before execution. When the AI uses reveal_columns, you see exactly which columns are being unmasked and can approve or deny the request. This creates a natural checkpoint — the AI proposes what to reveal, you decide whether to allow it.

PII detection

Detection uses a two-layer approach:

  • Column name heuristics (fast, no NLP) — Pattern matching on column names handles common patterns like email, first_name, phone, plus prefixed variants like bus_email, rep_phone_number, pref_first_name, former_last_name
  • Presidio NLP analysis (on first access) — Samples ~100 rows and runs Microsoft Presidio to detect PII in column values, catching columns with non-obvious names

Detected PII types include: email addresses, phone numbers, names, physical addresses, SSNs, tax IDs, credit cards, IP addresses, dates of birth, financial account numbers, geolocation, and more.

Secret columns (encrypted passwords, tokens, OTP secrets) are always fully redacted and cannot be revealed.

Free text columns (message bodies, notes, descriptions) get value-level Presidio scanning since PII is embedded in prose.

Masking styles

Style Example Description
partial (default) j***@e***.com Shows enough structure to be useful, hides the sensitive parts
full [EMAIL ADDRESS] Complete replacement with a type label
pseudonymize `user_a3f2@

Frequently Asked Questions

What are the key features of Postgres Safe?

Automatic PII detection using column-name heuristics and Microsoft Presidio NLP. Masking by default for sensitive data like emails, names, and SSNs. Selective unmasking capabilities via AI tool parameters. Hard-coded protection for secret columns like passwords and tokens. Human-in-the-loop approval for unmasking requests.

What can I use Postgres Safe for?

Allowing AI agents to perform data analysis on production databases without exposing customer PII. Safely debugging database issues by viewing masked query results. Generating reports from sensitive datasets while maintaining data privacy compliance.

How do I install Postgres Safe?

Install Postgres Safe by running: npx -y pg-redact-mcp

What MCP clients work with Postgres Safe?

Postgres Safe works with any MCP-compatible client including Claude Desktop, Claude Code, Cursor, and other editors with MCP support.

Conare · memory for coding agents

Turn this server into reusable context

Keep Postgres Safe docs, env vars, and workflow notes in Conare so your agent carries them across sessions.

Set up free$npx conare@latest