Codex CLI for PostgreSQL Development: MCP Servers, Schema Intelligence, Performance Tuning, and Agent-Driven Database Workflows

Codex CLI for PostgreSQL Development: MCP Servers, Schema Intelligence, Performance Tuning, and Agent-Driven Database Workflows


PostgreSQL remains the world’s most popular database for professional developers 1, yet until now this knowledge base lacked a dedicated article on using Codex CLI with the PostgreSQL MCP ecosystem. With PostgreSQL 18.4 shipping asynchronous I/O, virtual generated columns, and uuidv7() 2, and at least five mature MCP server implementations competing for developer attention, the tooling landscape has reached a tipping point where agent-driven database development is not merely possible — it is practical.

This article maps the PostgreSQL MCP ecosystem as it stands in May 2026, walks through configuring each major server with Codex CLI, and demonstrates real workflows for schema exploration, migration safety, query performance tuning, and production health monitoring.

The PostgreSQL MCP Ecosystem

Three tiers of MCP server have emerged for PostgreSQL, each targeting a different workflow:

Tier 1: Official Reference Server

The @modelcontextprotocol/server-postgres package from Anthropic’s MCP organisation provides the simplest entry point 3. It exposes a single query tool that executes read-only SQL within a READ ONLY transaction, plus automatic schema discovery as MCP resources. It suits quick schema inspection but lacks write support and performance tooling.

codex mcp add pg-reference -- \
  npx -y @modelcontextprotocol/server-postgres \
  'postgresql://dev:secret@localhost:5432/myapp'

Postgres MCP Pro (crystaldba/postgres-mcp) is the standout here, offering nine tools that span the full development lifecycle 4:

Tool Purpose
list_schemas Enumerate all schemas in the instance
list_objects Discover tables, views, sequences, extensions per schema
get_object_details Columns, constraints, indexes for a given object
execute_sql Run SQL with configurable read/write mode
explain_query Execution plans with hypothetical index simulation
get_top_queries Slowest queries via pg_stat_statements
analyze_workload_indexes Workload-wide optimal index recommendations
analyze_query_indexes Index suggestions for up to 10 specific queries
analyze_db_health Buffer cache, connections, vacuum health, replication lag

The index tuning engine implements a variant of the Anytime Algorithm from Microsoft’s Database Tuning Advisor, exploring thousands of candidate indexes to surface the optimal set 4.

codex mcp add pg-pro -- \
  uvx postgres-mcp \
  --connection-string 'postgresql://dev:secret@localhost:5432/myapp' \
  --access-mode unrestricted

pgEdge Postgres MCP Server adds natural language querying, multi-database switching, hybrid BM25+MMR semantic search, and custom tool definitions via YAML 5. It supports stdio, HTTP, and HTTPS transports with TLS and token authentication — making it the strongest candidate for enterprise environments.

codex mcp add pgedge -- \
  npx -y @pgedge/mcp-server-postgres \
  --connection-string 'postgresql://dev:secret@localhost:5432/myapp'

MCP-PostgreSQL-Ops targets the DBA persona with 30+ tools covering lock monitoring, autovacuum analysis, WAL status, replication lag, bloat detection, and PostgreSQL 18-specific async I/O stats 6. No extensions required for the core toolset.

Tier 3: Platform-Integrated Servers

Neon MCP Server wraps the Neon management API, letting agents create projects, branches, and databases conversationally 7. Its killer feature is copy-on-write branching: agents can fork a production-scale database in milliseconds, run destructive migrations on the branch, compare schemas with compare_database_schema, and discard the branch if things go wrong — all without touching production data.

codex mcp add neon -- \
  npx -y @neondatabase/mcp-server-neon
graph TD
    A[Codex CLI] --> B{MCP Router}
    B --> C["Reference Server<br/>Read-only queries"]
    B --> D["Postgres MCP Pro<br/>Schema + Performance"]
    B --> E["pgEdge<br/>Enterprise + NL queries"]
    B --> F["PostgreSQL-Ops<br/>DBA monitoring"]
    B --> G["Neon MCP<br/>Branch management"]

    D --> H[(PostgreSQL 18)]
    E --> H
    F --> H
    C --> H
    G --> I[(Neon Serverless)]

Configuring Codex CLI for PostgreSQL Workflows

Connection Configuration

Add your preferred MCP server to ~/.codex/config.toml or use codex mcp add as shown above 8. For projects that need both schema intelligence and performance tuning, stacking Postgres MCP Pro with Neon branching is a proven pattern:

[mcp_servers.pg-pro]
command = "uvx"
args = [
  "postgres-mcp",
  "--connection-string", "postgresql://dev:secret@localhost:5432/myapp",
  "--access-mode", "unrestricted"
]

[mcp_servers.neon]
command = "npx"
args = ["-y", "@neondatabase/mcp-server-neon"]
env = { NEON_API_KEY = "$NEON_TOKEN" }

Sandbox Considerations

Codex CLI’s default sandbox restricts network access. PostgreSQL MCP servers need to reach the database, so you have two options:

  1. Local socket: If PostgreSQL runs locally, mount the Unix socket directory in the sandbox.
  2. Network policy: Use network-egress-allowed in your permission profile when connecting to remote instances 8.
[permissions]
network-egress-allowed = ["localhost:5432", "*.neon.tech:5432"]

Agents.md Integration

Declare your database conventions in agents.md so Codex generates idiomatic SQL:

## Database Conventions
- PostgreSQL 18, all schemas in `public` unless noted
- Naming: snake_case tables and columns, `_id` suffix for primary keys
- UUIDs via `gen_random_uuid()` or `uuidv7()` for time-sorted keys
- Migrations managed by Drizzle ORM; never hand-edit migration files
- All queries must be explainable with cost < 1000 for OLTP paths

Workflow 1: Schema Exploration and Documentation

Ask Codex to map an unfamiliar database:

codex "Using pg-pro, list all schemas, then for each schema list all tables
with their columns, constraints, and indexes. Write the result as a Mermaid
ER diagram to docs/schema.md"

Codex chains list_schemaslist_objectsget_object_details for each table, then synthesises a Mermaid entity-relationship diagram. This replaces manual \d+ spelunking and produces version-controllable documentation.

Workflow 2: Safe Schema Migrations with Neon Branching

sequenceDiagram
    participant C as Codex CLI
    participant N as Neon MCP
    participant B as Branch DB
    participant P as Production DB

    C->>N: create_branch(parentId: main)
    N->>B: Copy-on-write fork
    C->>B: Run Drizzle migration
    C->>N: compare_database_schema(branch vs main)
    alt Schema diff acceptable
        C->>P: Apply migration to production
        C->>N: delete_branch
    else Unexpected changes
        C->>N: delete_branch
        C-->>C: Report diff, abort
    end

The copy-on-write branch takes milliseconds regardless of database size 7. The agent runs the migration on the branch, compares schemas, and only proceeds if the diff matches expectations. This pattern eliminates the “I hope this migration works” anxiety entirely.

Workflow 3: Query Performance Tuning

Postgres MCP Pro’s explain_query tool accepts hypothetical indexes, letting Codex explore optimisation strategies without creating real indexes:

codex "Find the top 5 slowest queries using pg-pro's get_top_queries,
then for each one run explain_query with and without suggested indexes
from analyze_query_indexes. Summarise the expected speedup for each."

The agent calls get_top_queries to identify bottlenecks via pg_stat_statements, then iterates through analyze_query_indexes and explain_query with hypothetical indexes to quantify improvements before any DDL touches the database 4.

Workflow 4: Production Health Monitoring

For scheduled health checks, combine MCP-PostgreSQL-Ops with Codex’s exec capabilities:

codex "Run a full health check: get_active_connections, get_autovacuum_status,
get_table_bloat_analysis for the 10 largest tables, get_replication_status,
and get_wal_status. Flag anything outside normal parameters."

On PostgreSQL 18, the get_async_io_status and get_per_backend_io_stats tools expose the new AIO subsystem metrics 6, which can surface I/O bottlenecks that were invisible in earlier versions.

Workflow 5: Multi-Database Environment Switching

pgEdge’s multi-database support lets agents compare schemas across environments:

codex "Connect to the staging database, get the schema for the orders table,
then switch to production and compare. Flag any schema drift."

This pattern catches deployment drift before it causes runtime errors — particularly valuable when multiple teams push migrations independently.

Security Considerations

Concern Mitigation
Credential exposure Use environment variables in MCP config, never inline connection strings in agents.md
Write access Default to --access-mode restricted (read-only); escalate only for migration workflows
Query injection MCP servers parameterise queries; Codex CLI hooks can gate execute_sql calls 8
Network scope Restrict network-egress-allowed to specific database hosts
Sensitive data Neon schema-only branches (beta) copy structure without rows 7

Choosing the Right Server

graph TD
    Q{What do you need?} --> A[Quick schema inspection]
    Q --> B[Full development lifecycle]
    Q --> C[DBA-grade monitoring]
    Q --> D[Branch-based migrations]
    Q --> E[Enterprise with NL queries]

    A --> F["Reference Server<br/>@modelcontextprotocol/server-postgres"]
    B --> G["Postgres MCP Pro<br/>crystaldba/postgres-mcp"]
    C --> H["MCP-PostgreSQL-Ops<br/>30+ DBA tools"]
    D --> I["Neon MCP Server<br/>Copy-on-write branching"]
    E --> J["pgEdge MCP Server<br/>TLS, tokens, YAML tools"]

For most Codex CLI users, Postgres MCP Pro hits the sweet spot: schema intelligence, configurable read/write, EXPLAIN with hypothetical indexes, and workload-level index tuning in a single server. Add Neon MCP if you need branch-based migration safety, or MCP-PostgreSQL-Ops if production monitoring is the primary concern.

What PostgreSQL 18 Changes for Agent Workflows

PostgreSQL 18’s headline features have direct implications for MCP-driven development 2:

  • Asynchronous I/O: Up to 3× faster sequential scans mean agent-triggered full-table analyses complete faster, making broader exploratory queries practical.
  • Virtual generated columns: Computed at query time rather than stored, reducing the schema complexity agents must reason about.
  • uuidv7(): Time-sortable UUIDs eliminate the need for composite indexes on (created_at, id) — agents can recommend simpler index strategies.
  • Skip scan on B-tree indexes: Multi-column index queries that omit prefix columns now perform dramatically better, expanding the viable index recommendations from tools like analyze_workload_indexes.
  • OAuth authentication: Enterprise SSO integration for database connections, simplifying credential management in MCP server configurations.

Citations

  1. Stack Overflow Developer Survey 2025 — PostgreSQL ranked #1 most popular database among professional developers. https://survey.stackoverflow.co/2025/ 

  2. PostgreSQL 18 Release Notes — Async I/O, virtual generated columns, uuidv7(), skip scan. https://www.postgresql.org/docs/release/18.0/  2

  3. @modelcontextprotocol/server-postgres — Official MCP reference server for PostgreSQL. https://www.npmjs.com/package/@modelcontextprotocol/server-postgres 

  4. Postgres MCP Pro — Configurable read/write access and performance analysis with hypothetical index simulation. https://github.com/crystaldba/postgres-mcp  2 3

  5. pgEdge Postgres MCP Server — Enterprise MCP server with NL queries, multi-database, TLS, and custom YAML tools. https://github.com/pgEdge/pgedge-postgres-mcp 

  6. MCP-PostgreSQL-Ops — 30+ DBA tools for performance analysis, bloat detection, lock monitoring, and PG 18 async I/O stats. https://github.com/call518/MCP-PostgreSQL-Ops  2

  7. Neon MCP Server — Serverless Postgres with copy-on-write branching, schema comparison, and schema-only branches. https://neon.com/docs/ai/neon-mcp-server  2 3

  8. Codex CLI MCP Configuration — Official documentation for adding MCP servers, permission profiles, and network policies. https://developers.openai.com/codex/mcp  2 3