{"record":{"id":"9235220ec4ccc198","repo":"nautechsystems/nautilus_trader","slug":"error-executing-statement-sql-statement-with-err","errorCode":null,"errorMessage":"Error executing statement {sql_statement} with error: {e:?}","messagePattern":"Error executing statement (.+?) with error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/pg.rs","lineNumber":508,"sourceCode":"                        if !statement.trim().is_empty() {\n                            statements.push(statement);\n                        }\n                        last_end = mat.end();\n                    }\n                    statements\n                }\n                _ => split_sql_statements(&sql_content),\n            };\n\n            for sql_statement in sql_statements {\n                if let Err(e) = sqlx::query(AssertSqlSafe(sql_statement.as_str()))\n                    .execute(&mut *connection)\n                    .await\n                {\n                    if e.to_string().contains(\"already exists\") {\n                        log::info!(\"Already exists error on statement, skipping\");\n                    } else {\n                        anyhow::bail!(\n                            \"Error executing statement {sql_statement} with error: {e:?}\"\n                        );\n                    }\n                }\n            }\n        }\n\n        Ok(())\n    }\n    .await;\n\n    let reset_result = sqlx::query(\"RESET ROLE;\").execute(connection).await;\n    match (result, reset_result) {\n        (Err(e), Err(reset_error)) => {\n            log::error!(\"Error resetting Postgres role after schema failure: {reset_error:?}\");\n            Err(e)\n        }\n        (Err(e), Ok(_)) => Err(e),","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/pg.rs#L490-L526","documentation":"execute_schema_as_role runs each SQL statement from the schema files against Postgres. Errors containing \"already exists\" are tolerated (skipped with an info log) to make schema execution idempotent, but any other execution error causes the function to bail with this message including the failing statement and the underlying database error.","triggerScenarios":"Executing schema SQL during init_postgres when a statement fails for reasons other than the object already existing: syntax errors, insufficient privileges for the role the statement is executed as, missing extensions/types referenced by the statement, or connection-level failures mid-execution.","commonSituations":"Running init against a Postgres user lacking CREATE privileges on the target schema/database; partially migrated databases where a dependent object was dropped manually; schema SQL version mismatched with the running Postgres version; SQLSTATE 42501 permission errors when executing as a restricted role.","solutions":["Read the embedded database error ({e:?}) in the message to identify the SQLSTATE and failing statement, then fix that statement or its privileges","Grant the executing role the required privileges (CREATE on schema/database, extension ownership) before rerunning init","Ensure the schema SQL files match your Postgres version and that prerequisites (extensions, roles) are created in order","If objects are in a broken partial state, drop the role/database (drop_postgres) and re-run init from a clean state"],"exampleFix":"// before: role lacks CREATE privilege -> bail\n\"Error executing statement CREATE TABLE order_events ... with error: DbError { code: \\\"42501\\\" ... }\"\n// after: grant privileges first\nGRANT CREATE ON SCHEMA public TO nautilus_db_role;","handlingStrategy":"try-catch","validationCode":"// pre-check privileges before running schema statements\nSELECT has_schema_privilege(current_user, 'public', 'CREATE');","typeGuard":null,"tryCatchPattern":"match init_postgres(pool).await {\n    Ok(_) => {},\n    Err(e) if e.to_string().contains(\"Error executing statement\") => {\n        // parse SQLSTATE from message; treat 42501 as privilege fix-up, retry after GRANT\n        log::error!(\"schema execution failed: {e}\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Grant the executing role CREATE privileges on the target schema/database before init","Run init as a superuser or role with sufficient DDL rights","Keep schema SQL files in sync with your Postgres version and initialize from a clean state when possible"],"tags":["database","postgres","sql","schema-migration"],"backgroundTag":"sql-query-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}