{"record":{"id":"cc54c28a58799f15","repo":"jdx/mise","slug":"failed-to-create-database-database","errorCode":null,"errorMessage":"failed to create database {database}","messagePattern":"failed to create database (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/daemons/presets.rs","lineNumber":248,"sourceCode":"                || !database\n                    .chars()\n                    .all(|c| c.is_ascii_alphanumeric() || c == '_')\n            {\n                bail!(\"invalid database name\");\n            }\n            let mut child = Command::new(\"postgres\")\n                .arg(\"--single\")\n                .arg(\"-D\")\n                .arg(&staged_data)\n                .arg(\"postgres\")\n                .stdin(Stdio::piped())\n                .spawn()?;\n            writeln!(\n                child.stdin.take().unwrap(),\n                \"CREATE DATABASE \\\"{database}\\\";\"\n            )?;\n            if !child.wait()?.success() {\n                bail!(\"failed to create database {database}\");\n            }\n        }\n        if std::fs::read_to_string(staged_data.join(\"PG_VERSION\"))?.trim() != major {\n            bail!(\"initdb and postgres have incompatible versions\");\n        }\n    } else {\n        std::fs::create_dir(&staged_data)?;\n    }\n    std::fs::write(staged_data.join(\".mise-daemon-version\"), marker)?;\n    std::fs::rename(staged_data, data)?;\n    Ok(())\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n    #[test]\n    fn presets_render_local_persistent_daemons() {","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/daemons/presets.rs#L230-L266","documentation":"mise's postgres daemon preset initializes a fresh data directory by running initdb and then, when the preset names a database other than the default 'postgres', spawning `postgres --single` with a piped CREATE DATABASE statement. If that single-user postgres process exits nonzero, mise gives up creating the database and bails with this message so the uninitialized staging directory is never promoted into place. It means the SQL submitted to the single-user server failed, typically due to an invalid database name or a broken postgres install.","triggerScenarios":"Running daemon preset initialization for a postgres preset whose database is not \"postgres\": `postgres --single -D <staged_data> postgres` is spawned, the SQL `CREATE DATABASE \"{database}\";` is piped to stdin, and child.wait() returns a non-success exit status.","commonSituations":"A daemon preset declares a database name that trips single-user-mode quirks (reserved names, quoting issues from odd characters that slipped past validation), or the postgres install is partially broken so the single-user backend crashes on startup.","solutions":["Check the database name in the daemon preset for invalid or problematic characters and use a simple ASCII identifier.","Verify the postgres installation works by running `postgres --version` and a manual `initdb`/single-user start; reinstall postgres if it crashes.","Remove any partially staged daemon data directory and re-run the mise command so initialization starts from a clean slate.","Run with MISE_DEBUG=1 to capture the underlying postgres stderr for the exact SQL failure."],"exampleFix":"// before\ndaemons.postgres.database = \"my-db-prod!\"\n\n// after\ndaemons.postgres.database = \"my_db_prod\"","handlingStrategy":"validation","validationCode":"// Validate the database name before configuring the daemon preset\nconst ok = /^[A-Za-z0-9_]+$/;\nif (!ok.test(database) || database === \"\") throw new Error(\"invalid database name for postgres preset\");","typeGuard":"fn is_valid_db_name(db: &str) -> bool {\n    !db.is_empty() && db.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')\n}","tryCatchPattern":"try {\n  mise daemons start\n} catch (e) {\n  if (String(e).includes(\"failed to create database\")) {\n    console.error(\"Check the preset's database name and your postgres install; inspect staged data dir.\");\n  }\n}","preventionTips":["Use simple ASCII identifiers (letters, digits, underscore) for preset database names","Verify `postgres --version` and a manual single-user run work before wiring daemon presets","Clean up leftover .mise-init-* staging directories after failed inits"],"tags":["postgres","daemon","database-creation"],"backgroundTag":"sql-query-failed","analyzedSha":"533346cc374382b41ec5ff70536252b2e96e725c","analyzedAt":"2026-09-17T13:35:38.149Z","contentChangedAt":"2026-09-17T13:35:38.149Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}