This guide walks through connecting PostgreSQL to Codex (OpenAI’s terminal coding agent) using QueryBear’s managed MCP server. End result: Codex sessions can query your Postgres database safely from the terminal.Documentation Index
Fetch the complete documentation index at: https://docs.querybear.com/llms.txt
Use this file to discover all available pages before exploring further.
What you’ll need
- A QueryBear account (sign up free)
- A PostgreSQL database (any version 12+)
- Codex CLI installed
Step 1: Create a read-only PostgreSQL role
Step 2: Add the connection to QueryBear
In the QueryBear dashboard → Connections → New connection → PostgreSQL, with the credentials from Step 1. Allow-list tables and block sensitive columns in the Access tab.Step 3: Add QueryBear to Codex
Edit~/.codex/config.toml (create it if it doesn’t exist) and add:
Step 4: Authorize and verify
Start a Codex session. The first tool call from Codex opens a browser for OAuth — approve, and Codex is linked to your QueryBear account. Ask:“What QueryBear tools do you have? List my connections.”You should see
list_connections, get_schema, run_query, and your Postgres connection.
Try it
“In the production Postgres database, find the 20 customers with the highest lifetime value. Pull the schema first.”Codex calls
get_schema to find customers, orders, etc., writes a SUM(amount) GROUP BY customer_id ORDER BY ... LIMIT 20 query, and runs it through QueryBear.
Postgres + Codex gotchas
- TOML syntax matters. A missing
type = "http"or a typo’d URL will silently disable the MCP server. Ifquerybeardoesn’t appear in the tool list, re-check the TOML. - Codex caches MCP discovery. If you change the config, restart the CLI fully.
- Naming the connection in your prompt skips a
list_connectionsround-trip and speeds responses: “using the prod connection, …” - For long-running analytics queries, raise the QueryBear query timeout for that connection — Codex’s default expectations are short.
Related
- PostgreSQL MCP server — Postgres-specific deep dive
- Codex client — Codex overview
- Security model — what the gateway protects against