{"record":{"id":"a9d831a116dbeb08","repo":"nikivdev/code","slug":"database-url-not-found-set-env-planetscale-datab","errorCode":null,"errorMessage":"DATABASE_URL not found (set env, PLANETSCALE_DATABASE_URL, or add it to {})","messagePattern":"DATABASE_URL not found \\(set env, PLANETSCALE_DATABASE_URL, or add it to (.+?)\\)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/storage.rs","lineNumber":127,"sourceCode":"\n    for key in [\n        \"DATABASE_URL\",\n        \"PLANETSCALE_DATABASE_URL\",\n        \"PSCALE_DATABASE_URL\",\n    ] {\n        if let Ok(url) = std::env::var(key) {\n            if !url.trim().is_empty() {\n                return Ok(url);\n            }\n        }\n    }\n\n    let env_path = project_dir.join(\".env\");\n    if let Some(value) = read_env_value(&env_path, \"DATABASE_URL\")? {\n        return Ok(value);\n    }\n\n    bail!(\n        \"DATABASE_URL not found (set env, PLANETSCALE_DATABASE_URL, or add it to {})\",\n        env_path.display()\n    )\n}\n\nfn read_env_value(path: &Path, key: &str) -> Result<Option<String>> {\n    if !path.exists() {\n        return Ok(None);\n    }\n    let contents = fs::read_to_string(path)\n        .with_context(|| format!(\"failed to read env file {}\", path.display()))?;\n    for line in contents.lines() {\n        let line = line.trim();\n        if line.is_empty() || line.starts_with('#') {\n            continue;\n        }\n        let line = line.strip_prefix(\"export \").unwrap_or(line);\n        let Some((name, value)) = line.split_once('=') else {","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/storage.rs#L109-L145","documentation":"resolve_database_url collects the Postgres connection string from (in order) environment variables (DATABASE_URL / PLANETSCALE_DATABASE_URL) and the .env file inside the project directory. When none of these sources contain DATABASE_URL it bails with this message telling the developer where it looked (the .env path is interpolated).","triggerScenarios":"Running postgres_migrate when: DATABASE_URL is unset in the shell, PLANETSCALE_DATABASE_URL is unset, and read_env_value finds no DATABASE_URL key in <project_dir>/.env (file missing, key missing, or unreadable).","commonSituations":"Fresh environment / CI machine with no .env committed (correctly gitignored); .env.example not copied to .env; variable named differently (POSTGRES_URL, SUPABASE_DB_URL) in .env; .env present in repo root but not in the resolved --project directory.","solutions":["export DATABASE_URL=postgres://user:pass@host:5432/db in your shell before running the command.","Add DATABASE_URL=... to the .env file in the project directory the CLI reports in the message.","Set PLANETSCALE_DATABASE_URL if using PlanetScale-managed credentials.","Copy .env.example to .env in the project dir and fill in the connection string."],"exampleFix":"// before (.env missing DATABASE_URL)\nPOSTGRES_URL=postgres://localhost/app\n// after\nDATABASE_URL=postgres://localhost:5432/app","handlingStrategy":"validation","validationCode":"import 'dotenv/config'; // or read <project>/.env manually\nif (!process.env.DATABASE_URL && !process.env.PLANETSCALE_DATABASE_URL) {\n  throw new Error('Set DATABASE_URL (env or <projectDir>/.env) before running migrations.');\n}","typeGuard":"function hasDatabaseUrl(env: NodeJS.ProcessEnv): env is NodeJS.ProcessEnv & { DATABASE_URL: string } {\n  return typeof env.DATABASE_URL === 'string' && env.DATABASE_URL.length > 0;\n}","tryCatchPattern":"try {\n  await postgresMigrate({ project });\n} catch (e) {\n  if (String(e).includes('DATABASE_URL not found')) {\n    console.error('Provide DATABASE_URL via env or .env in the project directory.');\n    process.exitCode = 1;\n    return;\n  }\n  throw e;\n}","preventionTips":["Commit a .env.example with DATABASE_URL and document copying it to .env during setup.","Export DATABASE_URL in CI secrets rather than relying on local .env files.","Keep the .env file inside the resolved --project directory, not just the repo root.","Standardize on the exact key name DATABASE_URL; avoid aliases like POSTGRES_URL."],"tags":["env","database","postgres","configuration","missing-env-var"],"backgroundTag":"missing-env-var","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}