|
| 1 | +--- |
| 2 | +title: Database migrations |
| 3 | +slug: /database-migrations |
| 4 | +--- |
| 5 | + |
| 6 | +Langflow stores application data such as flows, users, and API keys in a SQL database. |
| 7 | + |
| 8 | +When you upgrade Langflow or connect to a new empty database, Langflow must keep the database schema aligned with the application models. |
| 9 | + |
| 10 | +By default, the Langflow database uses [SQLite](https://www.sqlite.org/docs.html), and uses [Alembic](https://alembic.sqlalchemy.org/) for SQL database migrations. |
| 11 | + |
| 12 | +This page explains when migrations run and how to run and repair migrations safely. |
| 13 | + |
| 14 | +## Default migrations behavior and triggers |
| 15 | + |
| 16 | +Most Langflow users never need to run a database migration. |
| 17 | +When you start Langflow, or restart a container after upgrading the Langflow image, pending Alembic migrations run as part of database initialization. |
| 18 | + |
| 19 | +Langflow includes [Alembic revision scripts](https://github.com/langflow-ai/langflow/tree/main/src/backend/base/langflow/alembic) with the package. |
| 20 | +On startup, Langflow connects to the database at [`LANGFLOW_DATABASE_URL`](/memory#configure-external-memory) or the default `langflow.db` file, ensures the required tables exist, checks the database schema against the current models, and then applies any pending Alembic upgrades to bring the schema to its `head` revision. |
| 21 | + |
| 22 | +Alembic records the applied revision in the `alembic_version` table. |
| 23 | + |
| 24 | +Other events that trigger Alembic checks are a first Langflow startup against a new SQLite file or empty PostgreSQL database, upgrading to a Langflow version that includes new Alembic revisions, or pointing [`LANGFLOW_DATABASE_URL`](/memory#configure-external-memory) at an empty database. |
| 25 | + |
| 26 | +## Upgrade Langflow safely |
| 27 | + |
| 28 | +To upgrade Langflow or change the database URL in a deployment that already has data, you may need a database migration. |
| 29 | +To migrate your database, do the following: |
| 30 | + |
| 31 | +1. Stop Langflow. |
| 32 | +2. Back up the database. |
| 33 | + |
| 34 | + If you're using SQLite, copy the `langflow.db` file. |
| 35 | + The default path depends on your install method and whether `LANGFLOW_SAVE_DB_IN_CONFIG_DIR` is set. |
| 36 | + For more information, see [Memory management options](/memory#storage-options-and-paths). |
| 37 | + |
| 38 | + If you're using PostgreSQL, make a logical backup with `pg_dump`, or use your database provider's snapshot. |
| 39 | + For more information, see [enterprise backup guidance](/enterprise-database-guide). |
| 40 | + |
| 41 | +3. Export any flows you want to save as JSON. For more information, see [Import and export flows](/concepts-flows-import). |
| 42 | +4. Upgrade the Langflow package or container image to the latest version, keeping the same [`LANGFLOW_DATABASE_URL`](/memory#configure-external-memory) or the same SQLite path. |
| 43 | + |
| 44 | + ```bash |
| 45 | + uv pip install langflow -U |
| 46 | + ``` |
| 47 | + |
| 48 | +5. Start a **single** Langflow instance, so database migrations run without concurrent database writes. |
| 49 | +6. Confirm that startup succeeds. |
| 50 | +7. If startup reports a schema mismatch, run `langflow migration` to inspect the problem and restore from backup if needed. |
| 51 | + For more information, see [`langflow migration` CLI](/configuration-cli#langflow-migration). |
| 52 | + |
| 53 | +## Switch from SQLite to PostgreSQL |
| 54 | + |
| 55 | +Changing `LANGFLOW_DATABASE_URL` from SQLite to PostgreSQL does **not** copy existing SQLite data into PostgreSQL. |
| 56 | +Langflow initializes and migrates the new database schema, and your previous SQLite file remains where it was. |
| 57 | + |
| 58 | +There is no built-in SQLite-to-PostgreSQL data migrator. |
| 59 | +Langflow creates a new PostgreSQL schema, and then you can restore application data you saved. |
| 60 | + |
| 61 | +To migrate your flows while switching from SQLite to PostgreSQL, do the following: |
| 62 | + |
| 63 | +1. [Export flows](/concepts-flows-import), and note any [global variables](/configuration-global-variables) or credentials. |
| 64 | +2. Provision PostgreSQL 15+ and set `LANGFLOW_DATABASE_URL` as described in [Configure an external PostgreSQL database](/configuration-custom-database). |
| 65 | +3. If required, install PostgreSQL driver extras, for example `uv pip install "langflow[postgresql]"`. |
| 66 | +4. Start Langflow. The new PostgreSQL schema is created. |
| 67 | +5. Import the exported flows and recreate secrets or variables as needed. |
| 68 | + |
| 69 | +## Migration logging |
| 70 | + |
| 71 | +Alembic output is written to a log file by default at `alembic/alembic.log` under the Langflow config directory (`LANGFLOW_CONFIG_DIR`). |
| 72 | +Relative `LANGFLOW_ALEMBIC_LOG_FILE` values are also resolved against that config directory. |
| 73 | +In read-only or hardened containers, that path may not be writable, and Langflow will fall back to `stdout` and continue the migration. |
| 74 | + |
| 75 | +## Migration environment variables |
| 76 | + |
| 77 | +| Variable | Purpose | |
| 78 | +|----------|---------| |
| 79 | +| `LANGFLOW_ALEMBIC_LOG_TO_STDOUT` | Set to `true` to send Alembic migration output to `stdout`. | |
| 80 | +| `LANGFLOW_ALEMBIC_LOG_FILE` | Absolute or relative path for the Alembic log file when not logging to `stdout`. Relative paths resolve under the Langflow config directory. | |
| 81 | +| `LANGFLOW_MIGRATION_LOCK_TIMEOUT_S` | Seconds to wait for the PostgreSQL migration advisory lock. Default: `300`. | |
| 82 | +| `LANGFLOW_MIGRATION_LOCK_NAMESPACE` | Optional namespace for the Alembic PostgreSQL advisory lock. The default lock key coordinates instances that share a database. Set this variable to isolate different Langflow deployments that share one PostgreSQL server. Use the same value on every instance in that deployment. | |
| 83 | + |
| 84 | +## Troubleshooting |
| 85 | + |
| 86 | +For troubleshooting database migrations, see [Langflow upgrade issues](/troubleshoot#langflow-upgrade-issues). |
| 87 | + |
| 88 | +## See also |
| 89 | + |
| 90 | +* [Configure an external PostgreSQL database](/configuration-custom-database) |
| 91 | +* [Langflow database guide for enterprise DBAs](/enterprise-database-guide) |
| 92 | +* [Memory management options](/memory) |
| 93 | +* [`langflow migration` CLI](/configuration-cli#langflow-migration) |
| 94 | +* [Troubleshoot Langflow](/troubleshoot) |
| 95 | +* [Deploy Langflow on Docker](/deployment-docker) |
0 commit comments