Skip to main content
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.

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

CREATE ROLE querybear LOGIN PASSWORD 'choose-a-strong-one';
GRANT CONNECT ON DATABASE your_db TO querybear;
GRANT USAGE ON SCHEMA public TO querybear;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO querybear;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT SELECT ON TABLES TO querybear;

Step 2: Add the connection to QueryBear

In the QueryBear dashboardConnectionsNew connectionPostgreSQL, 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:
[mcp_servers.querybear]
type = "http"
url = "https://mcp.querybear.com/mcp"
Restart the Codex CLI.

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. If querybear doesn’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_connections round-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.