Summary
If you run gbrain from a project directory that has DATABASE_URL in its .env file (common in Next.js, Hono, Supabase, and most Node/bun web apps), bun automatically loads that .env and DATABASE_URL silently overrides the URL in ~/.gbrain/config.json. gbrain connects to the wrong database with no indication that a project .env is involved.
The error message points the user at the wrong place:
Cannot connect to database: Tenant or user not found.
Fix: Check your connection URL in ~/.gbrain/config.json
The URL in config.json is correct. The .env file is the culprit — but nothing says so.
Reproduction
- Have a working gbrain install with
~/.gbrain/config.json pointing at your brain (e.g. Supabase project A).
- Create a web project with a
.env containing DATABASE_URL=<some other postgres URL> (e.g. the app's own Supabase project B).
cd into that project and run gbrain stats.
- gbrain connects to project B (or fails with "Tenant or user not found" if project B does not exist), with no warning.
Environment
- OS: Windows 11 (also affects any OS where the project
.env has DATABASE_URL)
- Bun: 1.x (bun auto-loads
.env from CWD by default)
- gbrain: 0.18.2
- Brain backend: Supabase postgres pooler (port 6543)
Root cause
loadConfig() in src/core/config.ts:
const dbUrl = process.env.GBRAIN_DATABASE_URL || process.env.DATABASE_URL;
DATABASE_URL is used by virtually every web framework that talks to Postgres (Prisma, Drizzle, Supabase client, etc.). When bun loads the project .env, it injects DATABASE_URL into process.env before gbrain reads it, so it wins over config.json.
Why this is hard to debug
- The error says "Check your connection URL in
~/.gbrain/config.json" — config.json is fine.
- No indication that an env var is active or where it came from.
- Took ~2 hours to isolate by adding
console.log to loadConfig().
Suggested fix
When DATABASE_URL (not GBRAIN_DATABASE_URL) is the active source, emit a one-time warning:
[gbrain] Warning: DATABASE_URL env var is overriding ~/.gbrain/config.json.
This may be from a project .env file. Use GBRAIN_DATABASE_URL to
avoid conflicts with application databases.
Or drop the DATABASE_URL fallback entirely — too collision-prone for a tool running inside arbitrary project directories.
Workaround
Set GBRAIN_DATABASE_URL as a persistent env var (takes precedence over DATABASE_URL).
macOS/Linux:
export GBRAIN_DATABASE_URL="<your gbrain connection string>"
Windows:
[System.Environment]::SetEnvironmentVariable("GBRAIN_DATABASE_URL", "<url>", "User")
Also add to the gbrain MCP server env block in ~/.claude.json so it inherits correctly regardless of CWD.
Summary
If you run
gbrainfrom a project directory that hasDATABASE_URLin its.envfile (common in Next.js, Hono, Supabase, and most Node/bun web apps), bun automatically loads that.envandDATABASE_URLsilently overrides the URL in~/.gbrain/config.json. gbrain connects to the wrong database with no indication that a project.envis involved.The error message points the user at the wrong place:
The URL in
config.jsonis correct. The.envfile is the culprit — but nothing says so.Reproduction
~/.gbrain/config.jsonpointing at your brain (e.g. Supabase project A)..envcontainingDATABASE_URL=<some other postgres URL>(e.g. the app's own Supabase project B).cdinto that project and rungbrain stats.Environment
.envhasDATABASE_URL).envfrom CWD by default)Root cause
loadConfig()insrc/core/config.ts:DATABASE_URLis used by virtually every web framework that talks to Postgres (Prisma, Drizzle, Supabase client, etc.). When bun loads the project.env, it injectsDATABASE_URLintoprocess.envbefore gbrain reads it, so it wins overconfig.json.Why this is hard to debug
~/.gbrain/config.json" — config.json is fine.console.logtoloadConfig().Suggested fix
When
DATABASE_URL(notGBRAIN_DATABASE_URL) is the active source, emit a one-time warning:Or drop the
DATABASE_URLfallback entirely — too collision-prone for a tool running inside arbitrary project directories.Workaround
Set
GBRAIN_DATABASE_URLas a persistent env var (takes precedence overDATABASE_URL).macOS/Linux:
Windows:
Also add to the gbrain MCP server
envblock in~/.claude.jsonso it inherits correctly regardless of CWD.