> ## 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.

# PostgreSQL + Codex

> How to set up a secure PostgreSQL MCP server for OpenAI's Codex CLI using QueryBear. Add to ~/.codex/config.toml. Step-by-step with read-only role and example queries.

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](https://querybear.com/signup))
* A PostgreSQL database (any version 12+)
* Codex CLI installed

## Step 1: Create a read-only PostgreSQL role

```sql theme={null}
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 dashboard](https://querybear.com/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:

```toml theme={null}
[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.

## Related

* [PostgreSQL MCP server](/databases/postgres) — Postgres-specific deep dive
* [Codex client](/clients/codex) — Codex overview
* [Security model](/features/security) — what the gateway protects against
