New teams get free Claude credits for their trial. Learn more

Add Postgres to Codex as an MCP server

October 22, 2026•5 min read•Vivek Sah
Add Postgres to Codex as an MCP server

Codex runs local MCP servers, so Postgres is a short config away. Run a read-only Postgres MCP server on your machine, or connect the database to Contextflo and give Codex one URL. Either way, make a read-only role first.

Make a read-only role

Both options start here. Point it at a read replica if you have one.

CREATE ROLE contextflo_ro LOGIN PASSWORD '<generated>';
GRANT CONNECT ON DATABASE mydb TO contextflo_ro;
GRANT USAGE ON SCHEMA public TO contextflo_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO contextflo_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO contextflo_ro;

Option 1: a local Postgres MCP server in Codex

@contextflo/postgres-mcp is MIT licensed and enforces read-only in four layers. Run it with npx:

npx -y @contextflo/postgres-mcp postgresql://contextflo_ro:pass@replica-host:5432/mydb

In ~/.codex/config.toml (or a project .codex/config.toml):

[mcp_servers.postgres]
command = "npx"
args = ["-y", "@contextflo/postgres-mcp", "postgresql://contextflo_ro:pass@replica-host:5432/mydb"]

@bytebase/dbhub is an alternative. Skip @modelcontextprotocol/server-postgres. It is archived and has a SQL-injection bypass.

Option 2: Contextflo

In Contextflo, go to Data Sources, then Add data source, and choose Postgres. Paste a postgresql:// connection string for the read-only role. It works with RDS, Supabase, Neon, Cloud SQL, Azure, Railway, Render and self-hosted databases. Pick the tables and invite your team.

In Codex:

[mcp_servers.contextflo]
url = "https://mcp.contextflo.com/mcp"
codex mcp login contextflo

codex mcp list shows what is connected. Contextflo runs SELECT only.

Which one to use

Just you, in your own terminal: Option 1.

Your team asks too: Contextflo. It writes table and column descriptions, your team saves shared definitions, each person gets their own table access, and you get an audit trail of prompts and the SQL that ran. pg_stat_statements has query text, not the person.

More servers compared in best Postgres MCP servers. On Claude? See Claude with Postgres.

You do the modeling, we do the rest. Connect Postgres free.

FAQ

Can Codex connect to a local Postgres MCP server? Yes. Codex runs local stdio servers. Add a [mcp_servers.name] table with command and args to ~/.codex/config.toml, and put credentials in [mcp_servers.name.env] when the server needs them.

Should I use the official modelcontextprotocol Postgres server with Codex? No. @modelcontextprotocol/server-postgres is archived and has a known SQL-injection bypass. Use @contextflo/postgres-mcp or @bytebase/dbhub instead.

Which Postgres hosts work with Contextflo? RDS, Supabase, Neon, Cloud SQL, Azure, Railway, Render and self-hosted Postgres. Contextflo takes a postgresql:// connection string.

Why point Codex at a read replica? An agent can write expensive queries. A replica keeps them away from your primary. Pair it with a read-only role so nothing can write.