{"record":{"id":"0cb894d11aad6d26","repo":"NationalSecurityAgency/ghidra","slug":"error-initializing-postgres-database","errorCode":null,"errorMessage":"Error initializing postgres database","messagePattern":"Error initializing postgres database","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/BSimControlLaunchable.java","lineNumber":909,"sourceCode":"\t\tcommand.add(\"-o\");\n\t\tcommand.add(\"'--username=\" + connectingUserName + '\\'');\n\t\tif (hostAuthentication == AUTHENTICATION_PASSWORD) {\n\t\t\testablishAdminPassword();\n\t\t\tcommand.add(\"-o\");\n\t\t\tcommand.add(\"'--pwfile=\" + passwordFile.getAbsolutePath() + '\\'');\n\t\t}\n\t\telse if (hostAuthentication == AUTHENTICATION_PKI) {\n\t\t\tif (commonName == null) {\n\t\t\t\tthrow new GeneralSecurityException(\n\t\t\t\t\t\"Distinguished name option (--dn) required for \" + connectingUserName);\n\t\t\t}\n\t\t\tcheckCertAuthorityFile();\n\t\t}\n\t\tcommand.add(\"-D\");\n\t\tcommand.add(dataDirectory.getAbsolutePath());\n\t\tint res = runCommand(null, command, loadLibraryVar, loadLibraryValue);\n\t\tif (res != 0) {\n\t\t\tthrow new IOException(\"Error initializing postgres database\");\n\t\t}\n\t\tFile configCopy = new File(dataDirectory, POSTGRES_CONFIGFILE + \".orig\");\n\n\t\tif (hostAuthentication == AUTHENTICATION_PKI || localAuthentication == AUTHENTICATION_PKI) {\n\t\t\tFile rootCA = new File(dataDirectory, POSTGRES_ROOTCA);\n\t\t\tFileUtilities.copyFile(certAuthorityFile, rootCA, false, null);\n\t\t\taddCertificateName(connectingUserName);\n\t\t}\n\n\t\t// Move the original configuration file\n\t\tif (!configFile.renameTo(configCopy)) {\n\t\t\tthrow new IOException(\"Error copying original configuration file\");\n\t\t}\n\n\t\tFile hbaCopy = new File(dataDirectory, POSTGRES_CONNECTFILE + \".orig\");\n\n\t\t// Move the original connection file\n\t\tif (!hbaFile.renameTo(hbaCopy)) {","sourceCodeStart":891,"sourceCodeEnd":927,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/BSimControlLaunchable.java#L891-L927","documentation":"Thrown by initializeDataDirectory() when `pg_ctl init` (PostgreSQL cluster initialization) returns a non-zero exit code. This means PostgreSQL itself refused to initialize the data directory — BSim only reports the aggregate failure, not the postgres detail.","triggerScenarios":"The constructed `pg_ctl init -o '--username=...' [-o '--pwfile=...'] -D <dataDir>` command exits non-zero. Common when the data directory is non-empty, lacks permissions, or the postgres binaries/version are wrong.","commonSituations":"Re-running `bsim_ctl start` against a data directory that was already initialized; data directory owned by another user / not writable; mismatched postgres major version vs existing cluster files; missing LD_LIBRARY_PATH pointing at postgres shared libs (loadLibraryVar); leftover files from a failed prior init.","solutions":["Read the postgres log/stderr from the runCommand output to get the real reason.","Point at an empty or freshly-created data directory for a first-time init.","Ensure the OS user running bsim_ctl owns and can write the data directory.","Verify the postgres installation discovered by discoverPostgresInstall() and that loadLibraryVar/loadLibraryValue resolve the shared libraries.","If the directory is already initialized, do not re-init — start directly."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-flight: ensure a clean, writable, empty cluster dir before init.\nFile d = new File(dataDir);\nif (d.exists() && d.list().length > 0) {\n    throw new IllegalStateException(\n        \"Data directory not empty; pg_ctl init will likely fail: \" + d);\n}","typeGuard":"public boolean isInitableClusterDir(File d) {\n    return d != null && d.isDirectory() && d.canWrite()\n        && (d.list() == null || d.list().length == 0);\n}","tryCatchPattern":"try {\n    bsimControl.start(args);\n} catch (IOException e) {\n    if (\"Error initializing postgres database\".equals(e.getMessage())) {\n        // capture <dataDir>/log output and surface it; do NOT silently retry.\n        throw new UserFacingException(\"pg_ctl init failed; see data dir log\", e);\n    }\n    throw e;\n}","preventionTips":["Never re-init an existing cluster; use start against an initialized dir.","Confirm ownership/perms and the postgres install before init.","Always read the postgres log on init failure rather than retrying blind."],"tags":["postgresql","initialization","process","bsim","cli","environment"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}