cognee.run_startup_migrations
Applies all pending database schema migrations before the rest of your application starts.
await cognee.run_startup_migrations()
It runs two steps in sequence:
- Relational schema — executes
alembic upgrade head against your configured relational database (SQLite by default, or Postgres).
- Vector schema — calls the vector adapter’s own migration method if one exists (e.g., creates required collections or indexes). If the active vector engine has no migration method, this step is silently skipped.
When to call it
| Scenario | Why |
|---|
After upgrading the cognee package | New versions may add tables or columns to the relational schema. |
| First run against an external database | SQLite is auto-migrated on startup; Postgres and other external databases are not. |
| Kubernetes / Docker init containers | Run migrations once before starting the main application pods. |
| Switching to a new relational DB provider | The new database starts empty and needs all migrations applied. |
For the default local setup (SQLite + LanceDB), Cognee handles migrations automatically. You only need to call run_startup_migrations() explicitly in server deployments or CI pipelines where you manage database lifecycle yourself.
Example
import asyncio
import cognee
async def main():
# Apply all pending schema migrations before starting
await cognee.run_startup_migrations()
# Normal usage
await cognee.add("Hello, world!", dataset_name="demo")
await cognee.cognify()
asyncio.run(main())
Kubernetes init container
Run migrations as a one-shot init container so the main pod only starts after the schema is ready:
initContainers:
- name: migrate
image: your-cognee-image:latest
command: ["python", "-c", "import asyncio, cognee; asyncio.run(cognee.run_startup_migrations())"]
envFrom:
- secretRef:
name: cognee-env
Errors
| Error | Cause | Fix |
|---|
FileNotFoundError | alembic.ini or the migrations directory is missing from the installed package. | Reinstall cognee — the package may be corrupted or partially installed. |
MigrationError | Alembic exited with a non-zero return code. | Check the error message logged at ERROR level; usually a DB connection problem or a SQL conflict. |
Set LOG_LEVEL=DEBUG to see the full Alembic output when diagnosing migration failures.