nautechsystems/nautilus_trader · error
Error executing statement {sql_statement} with error: {e:?}
Error message
Error executing statement {sql_statement} with error: {e:?} What it means
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.
Source
Thrown at crates/infrastructure/src/sql/pg.rs:508
if !statement.trim().is_empty() {
statements.push(statement);
}
last_end = mat.end();
}
statements
}
_ => split_sql_statements(&sql_content),
};
for sql_statement in sql_statements {
if let Err(e) = sqlx::query(AssertSqlSafe(sql_statement.as_str()))
.execute(&mut *connection)
.await
{
if e.to_string().contains("already exists") {
log::info!("Already exists error on statement, skipping");
} else {
anyhow::bail!(
"Error executing statement {sql_statement} with error: {e:?}"
);
}
}
}
}
Ok(())
}
.await;
let reset_result = sqlx::query("RESET ROLE;").execute(connection).await;
match (result, reset_result) {
(Err(e), Err(reset_error)) => {
log::error!("Error resetting Postgres role after schema failure: {reset_error:?}");
Err(e)
}
(Err(e), Ok(_)) => Err(e),View on GitHub (pinned to 18893faf8b)
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
Example fix
// before: role lacks CREATE privilege -> bail
"Error executing statement CREATE TABLE order_events ... with error: DbError { code: \"42501\" ... }"
// after: grant privileges first
GRANT CREATE ON SCHEMA public TO nautilus_db_role; Defensive patterns
Strategy: try-catch
Validate before calling
// pre-check privileges before running schema statements SELECT has_schema_privilege(current_user, 'public', 'CREATE');
Try / catch
match init_postgres(pool).await {
Ok(_) => {},
Err(e) if e.to_string().contains("Error executing statement") => {
// parse SQLSTATE from message; treat 42501 as privilege fix-up, retry after GRANT
log::error!("schema execution failed: {e}");
}
Err(e) => return Err(e),
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.
Related errors
- Failed to read verification schema version: {e}
- Failed to read canonical nonce ledger: {e}
- Failed to load account ids: {e}
- Failed to insert into trade table: {e}
- Failed to load trades: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/9235220ec4ccc198.
Report an issue: GitHub.