Migrate from MongoDB to PostgreSQL
Prepare a DeepFellow Server install that still runs on MongoDB for PostgreSQL, and move its data with or without DeepFellow CLI.
DeepFellow Server moves from MongoDB to PostgreSQL. A new install runs on PostgreSQL. An install made with an earlier version keeps its data in MongoDB and moves it to PostgreSQL once. The DF_DB_BACKEND setting in .env selects the database: postgres, or mongo, which is deprecated. An install without DF_DB_BACKEND runs on MongoDB.
The move spans three releases:
| Release | DeepFellow Server | Migration |
|---|---|---|
| 0.42 | Runs on PostgreSQL or MongoDB. On MongoDB, it warns that MongoDB is deprecated. | Optional: deepfellow server database migrate with DeepFellow CLI, or the server image without the CLI. |
| 0.43 | Runs on PostgreSQL only and refuses to start on MongoDB. | Required: deepfellow server update runs it. Without the CLI, the server image runs it. |
| 0.44 | Runs on PostgreSQL only. | The migration tools are removed. Migrate with 0.43 before you update to 0.44. |
The migration takes a verified snapshot of MongoDB, rehearses the migration with a dry run, and copies the data into PostgreSQL with the cutover tool, which verifies the copy. The cutover only reads MongoDB, so your MongoDB data stays available for a rollback.
The migration moves the data that DeepFellow Server keeps in MongoDB. The embeddings in the vector database and the uploaded files stay where they are.
A new install doesn't need a migration. deepfellow server install and deepfellow suite install set up PostgreSQL. See Install DeepFellow Server.
Prepare for PostgreSQL
MongoDB Deprecation Warning
On an install that still runs on MongoDB, DeepFellow Server works as before and warns that MongoDB is deprecated:
- At startup, the server and Nightshift log a deprecation warning. The warning ends with the PostgreSQL readiness:
not_configuredwhileDF_POSTGRES_PASSWORDis empty or a placeholder,unreachablewhen the server cannot connect to the configured PostgreSQL, orready. The check doesn't delay the startup and is logged only. GET /inforeturns anoticesentry with the codemongodb_deprecatedandrequired_inset to"0.43.0", the version that requires PostgreSQL.- The
db_backend_info{backend}metric reportsbackend="mongo", and theDfServerOnDeprecatedMongoDbalert rule fires on it. See Observability.
The warning means that DeepFellow Server 0.43 doesn't start on this install until its data is in PostgreSQL. Nothing changes in 0.42 until you migrate.
Checklist
Before the migration, check the following:
- PostgreSQL: Decide whether the install gets a local PostgreSQL 19, which DeepFellow CLI adds, or uses an existing external PostgreSQL 19, for example one that already has your monitoring and backups. An external PostgreSQL needs a role and an empty database that an administrator creates. See Use an External PostgreSQL.
- Credentials: The
DF_POSTGRES_*settings need a real password. DeepFellow Server refuses to start on PostgreSQL whenDF_POSTGRES_PASSWORDis empty,change-me, orsecret. For every PostgreSQL setting, read Configuration. - Disk space: The snapshot directory,
~/.deepfellow/db-snapshotsby default, needs free space of at least twice the size of the MongoDB data. - Readiness: With DeepFellow CLI, run
deepfellow server database check. The command changes nothing. It checks the free disk space and rehearses the migration with a dry run in a temporary PostgreSQL container, which reports any document that doesn't map to PostgreSQL. It doesn't check an external PostgreSQL;deepfellow server database migratechecks it before it stops the server. Seedeepfellow server database. - Maintenance window: The migration stops DeepFellow Server and Nightshift. The more data MongoDB holds, the longer the migration takes.
If you installed DeepFellow Server with DeepFellow CLI, you can migrate in 0.42. See Migrate with DeepFellow CLI. If you don't migrate in 0.42, deepfellow server update to 0.43 runs the migration.
If you run the server image with Docker Compose without the CLI, you can migrate in 0.42 too. See Migrate Without DeepFellow CLI. If you don't migrate in 0.42, keep DF_DB_BACKEND unset or mongo, and migrate when you update to 0.43.
Migrate with DeepFellow CLI
Requirements
- An install made with
deepfellow server installordeepfellow suite install, on DeepFellow Server 0.42. - The current DeepFellow CLI. To update it, run
deepfellow cli update. - Free disk space in the snapshot directory,
~/.deepfellow/db-snapshotsby default, of at least twice the size of the MongoDB data. - A maintenance window. The command stops DeepFellow Server and Nightshift for the whole migration.
- To migrate to an existing PostgreSQL instead of a local one: a PostgreSQL 19 server with a role and an empty database that the role owns. See Use an External PostgreSQL.
You don't need a checkout of the DeepFellow Server repository or the MongoDB Database Tools on the host. The CLI runs the migration tools in one-off containers of the server image.
Run the Migration
To check the install first, run:
deepfellow server database checkTo migrate, run:
deepfellow server database migratedeepfellow server update to 0.42 also offers to migrate. The default answer is no, which updates the server and keeps MongoDB. See PostgreSQL Migration.
Before it changes anything, the command prints the size of the MongoDB data and the free disk space in the snapshot directory. If the free disk space is below twice the size of the MongoDB data, the command stops and changes nothing. Free some space, or choose another directory with --snapshot-dir, and run the command again. The command then asks for confirmation.
Unless you pass --postgres-url, the command then asks where to move the data:
❓ Add a local PostgreSQL for DeepFellow Server? (No: use an existing external one) [y/n] (y):Answer y to add a local PostgreSQL to the install. Answer n to use an existing PostgreSQL. The command then asks for its host:port, username, and password. See Use an External PostgreSQL. The default answer is y, or n if an earlier migration attempt of this install used an external PostgreSQL. With --non-interactive, the command takes the default answer.
The command then runs the migration and prints each step as [n/N]:
Add PostgreSQL to the install: a local PostgreSQL 19 with generated credentials, a data volume, and a health check, or the external PostgreSQL. The command checks that DeepFellow Server can use an external PostgreSQL before it stops the server.
Stop DeepFellow Server and Nightshift.
Start the databases.
Prepare the MongoDB data. This step applies the pending MongoDB schema migrations to MongoDB.
Take a snapshot of MongoDB and verify it.
Rehearse the migration with a dry run.
Run the cutover, which copies the data into PostgreSQL and verifies it.
Switch the install to PostgreSQL.
Start DeepFellow Server.
The command switches the install to PostgreSQL only after the cutover succeeds. The switch does the following:
- Sets
DF_DB_BACKEND=postgresin.env. - Removes the
mongoservice fromcompose.yaml. Its data volume stays. - Moves the MongoDB settings from
.envtomongo-rollback.envin the install directory, together with the server image and the path of the snapshot. See MongoDB Settings after the Migration.
The command saves the output of the dry run and the cutover in a report next to the snapshot, mongo-TIMESTAMP.archive.report.txt, readable by its owner only. At the end, it prints the paths of the snapshot and the report.
The snapshot holds password hashes, tokens, and webhook secrets. The command creates it readable by its owner only. Store it with the same care as the database itself.
Use an External PostgreSQL
DeepFellow CLI and DeepFellow Server don't create the role and the database on an existing PostgreSQL. A local PostgreSQL creates them at its first start. An external one doesn't. Before the migration, ask the administrator of the PostgreSQL server to create them, for example:
CREATE ROLE deepfellow LOGIN PASSWORD 'PASSWORD';
CREATE DATABASE deepfellow OWNER deepfellow;The role needs permission to create tables in the public schema of the database. The owner of the database has this permission. The database has to be empty: the cutover refuses a PostgreSQL that holds rows.
DeepFellow Server connects to the PostgreSQL from its container, so the host:port you give has to be reachable from the Docker network of the install, not only from the host.
Right after it adds the PostgreSQL settings to the install, and before it stops DeepFellow Server, the command connects to the PostgreSQL with them from a one-off container of the server image. It stops and puts back the previous setup if any of the following is true:
- The PostgreSQL is unreachable.
- The login fails, for example because the role doesn't exist or the password is wrong.
- The database doesn't exist.
- The role has no permission to create tables in the
publicschema.
The command prints the reason. The server keeps running on MongoDB. Fix the cause and run the migration again.
To pass the PostgreSQL in the command instead of answering the questions, use the --postgres-* options:
deepfellow server database migrate --postgres-url db.example.com:5432 --postgres-username deepfellow --postgres-password PASSWORDNon-Interactive Migration
To migrate unattended, for example from a script, pass the global --yes option, which answers the confirmation:
deepfellow --yes --non-interactive server database migrateWith --non-interactive and without --yes, the command exits with status 1 before it changes anything.
To migrate as part of the update to 0.42, pass --migrate-database to deepfellow server update or deepfellow update:
deepfellow --non-interactive server update --migrate-databaseWithout --postgres-url, a non-interactive migration adds a local PostgreSQL, or uses the external PostgreSQL of an earlier migration attempt. To migrate to an external PostgreSQL unattended, pass --postgres-url, --postgres-username, and --postgres-password.
The following options of deepfellow server database migrate apply to the migration:
| Option | Default | Description |
|---|---|---|
--snapshot-dir PATH | ~/.deepfellow/db-snapshots | Directory for the MongoDB snapshot, kept after the migration. |
--postgres-url TEXT | postgres:5432 | The host:port of an existing PostgreSQL to migrate to. The default asks whether to add a local one. |
--postgres-port INTEGER | 5432 | Host port to publish the local PostgreSQL on. |
--postgres-database-name TEXT | deepfellow | Name of the PostgreSQL database. |
--postgres-username TEXT | PostgreSQL username. Generated for a local PostgreSQL when empty, asked for an external one. | |
--postgres-password TEXT | PostgreSQL password. Generated for a local PostgreSQL when empty, asked for an external one. | |
--allow-drift | Migrate even if MongoDB changed after the snapshot. The migration leaves out the later changes. | |
--skip-unmappable | Migrate even if some documents do not map to the PostgreSQL schema. The migration skips those documents. | |
--wipe-postgres | Delete what PostgreSQL holds from an earlier migration without asking, for example after a rollback. |
deepfellow server update and deepfellow update accept the same options, and --migrate-database. For every option, read deepfellow server database.
If the Migration Fails
If a step fails, or you stop the command with Ctrl+C, SIGTERM, or SIGHUP (for example, a closed SSH session), the command puts back the MongoDB setup and starts the server on MongoDB again. It prints the reason for the failure. If the cutover started, it also prints the path of the report with the output of the dry run and the cutover.
The MongoDB data stays unchanged, except for the pending MongoDB schema migrations that the preparation step applied. The command tells you whether that step ran.
If the dry run or the cutover refused to run, the command prints the likely ways forward:
- Documents that do not map to PostgreSQL: The dry run lists them in
UNMAPPABLEandUNMAPPEDlines, for example a collection left over from an older version, or a Project whose Organization no longer exists. Fix them in MongoDB, or pass--skip-unmappableto leave them out of the migration. - MongoDB changed after the snapshot: Something still wrote to MongoDB. Make sure that nothing else uses this MongoDB, and run the migration again. To leave the later changes out instead, pass
--allow-drift. - PostgreSQL already holds rows: The rows don't come from a migration of this install. Migrate to an empty database, for example with
deepfellow server database migrate --postgres-database-name.
To try again, fix the cause, and run deepfellow server database migrate again, with --skip-unmappable or --allow-drift if needed:
deepfellow server database migrate --skip-unmappableIf the Migration Is Cut Off
A hard stop skips the step that puts back the previous setup. A hard stop is a power loss, kill -9, or a second Ctrl+C while the command puts back the previous setup. A normal Ctrl+C, SIGTERM, or SIGHUP doesn't need the following steps: the command puts back the previous setup itself.
To recover, check DF_DB_BACKEND in the .env file of the install, ~/.deepfellow/server/.env by default:
DF_DB_BACKENDis unset ormongo: The migration stopped before it switched the install. Rundeepfellow server database migrateagain, or rundeepfellow server startto keep running on MongoDB.DF_DB_BACKEND=postgres: The migration already switched the install to PostgreSQL. Rundeepfellow server start.
If the cutover started before the hard stop, PostgreSQL holds partial data. The next migration asks before it deletes that data. To skip the question, pass --wipe-postgres.
Check the Status
To see which database the install uses and what a migration kept, run:
deepfellow server database statusThe command changes nothing. It prints the following:
- The database the install uses
- The server image
- Whether a rollback to MongoDB is possible, and if not, why
- The path, age, and size of the MongoDB snapshot
- The data volumes kept from a migration or a rollback, with their sizes
Roll Back to MongoDB
Rolling back means running DeepFellow Server on the MongoDB data, which the migration left untouched. DeepFellow Server 0.42 still runs on MongoDB with DF_DB_BACKEND=mongo. Use the rollback as a temporary fallback if the migration causes a problem. To roll back a migrated install, run:
deepfellow server database rollbackThe command stops DeepFellow Server, puts back the MongoDB settings and service and the server image recorded in mongo-rollback.env, sets DF_DB_BACKEND=mongo, and starts the server. The server then uses the data that MongoDB had at the migration.
The rollback discards anything written to PostgreSQL after the migration. The command doesn't copy it back to MongoDB. The command warns about this and asks you to confirm. To roll back without the question, pass --discard-postgres-writes. The global --yes option does not answer it.
The rollback keeps the PostgreSQL data volume and its settings in postgres-rollback.env. A later deepfellow server database migrate migrates again from the current MongoDB data. It asks before it deletes what PostgreSQL holds. To skip that question, pass --wipe-postgres.
DeepFellow Server 0.43 doesn't run on MongoDB, so the rollback is a fallback in 0.42 only.
Remove the Kept Data
Once you no longer need a rollback, remove what the migration kept:
deepfellow server database cleanupThe command lists what it removes and asks for confirmation:
- The MongoDB data volume
- The MongoDB snapshot, its verification marker, and its report
mongo-rollback.env- The reports of failed migrations
To remove them without the question, pass --force. The global --yes option does not answer the question. If a failed migration used another snapshot directory, pass it with --snapshot-dir to find its reports.
After the cleanup, a rollback to MongoDB is no longer possible. The snapshot and the MongoDB volume are the only copies of the MongoDB data. Keep them for at least one release after the migration.
On an install that still uses MongoDB, deepfellow server database cleanup removes only the reports of failed migrations. If there are none, it exits with an error.
deepfellow server uninstall doesn't remove a database volume and a snapshot that a migration or a rollback kept. It saves the credentials of a kept local volume in the snapshot directory and prints the volume name. deepfellow server prune removes the kept volumes.
Migrate Without DeepFellow CLI
Use the following procedures if you run the DeepFellow Server image with Docker Compose without DeepFellow CLI. The 0.42 server image ships everything the migration needs: the migration tools and the MongoDB Database Tools. There are two ways to run the migration:
- Automatically at server start: the server moves the data itself before it starts serving on PostgreSQL.
- With one command: a one-off server container moves the data, and you switch the server to PostgreSQL afterwards.
Both run the same step. It prepares the MongoDB data, takes a snapshot of MongoDB as a backup, checks with checksums taken before and after the snapshot that MongoDB didn't change meanwhile, copies the data into PostgreSQL, and verifies it. Once PostgreSQL holds the verified data, the step does nothing, so it is safe to run again.
In 0.42 the migration is optional. Until you set DF_DB_BACKEND=postgres, the server keeps running on MongoDB.
Before You Migrate
Update the server and Nightshift to 0.42 first, then finish the following checklist:
- Stop everything that writes to MongoDB. Stop DeepFellow Server and Nightshift, and keep them stopped until the migration is done. If MongoDB changes after the snapshot, the migration refuses to finish. A Nightshift container left running is the most common cause.
- Prepare PostgreSQL. You need a PostgreSQL 19 server with an empty database for DeepFellow Server. Set the
DF_POSTGRES_*variables in.env. The server creates and upgrades the schema itself. To run PostgreSQL with Docker Compose, see Preparecompose.yaml. For every PostgreSQL variable, read Configuration. - Keep the MongoDB settings. The
DF_MONGO_*variables and the MongoDB service stay. The migration reads MongoDB with them, and a rollback needs them. - Provide storage for the snapshot. Choose a directory on a volume that survives the container, with free space of at least the size of the MongoDB data. The migration keeps the snapshot of a finished migration for a rollback.
- Plan a maintenance window. The more data MongoDB holds, the longer the migration takes.
Prepare compose.yaml
The settings in .env reach the containers only through compose.yaml. Check the following in yours:
- The
serverandnightshiftservices get the settings from.env. Docker Compose uses.envonly to fill in${...}values incompose.yaml. A container gets only the variables that itsenv_file:orenvironment:lists. Addenv_file: .envto both services, or listDF_DB_BACKEND, theDF_POSTGRES_*variables, and theDF_MONGO_MIGRATION__*variables in theirenvironment:. Nightshift needsDF_MONGO_MIGRATION__ENABLEDto wait for the migration. Without these variables, the server stays on MongoDB. To check, rundocker compose configand look at theenvironmentof both services. - A PostgreSQL service, unless you use an external PostgreSQL server. Run it on the same Docker network as the server, with a volume for its data and a health check, and make
serverandnightshiftdepend on it. - A snapshot directory on a volume that survives the container. A directory on a volume that the server already mounts is enough. The examples below use
/app/storage/mongo-snapshots, on the volume that holds the server's storage at/app/storage. You don't need a new mount for it.
A compose.yaml written by DeepFellow CLI has no env_file: and no PostgreSQL service. Put the changes in a compose.override.yaml file next to it. Docker Compose merges that file into compose.yaml when you run docker compose in that directory, and the file stays when the CLI writes compose.yaml again. For example:
services:
postgres:
image: postgres:19.0
restart: always
environment:
POSTGRES_USER: ${DF_POSTGRES_USER}
POSTGRES_PASSWORD: ${DF_POSTGRES_PASSWORD}
POSTGRES_DB: ${DF_POSTGRES_DB}
volumes:
- postgres:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", 'pg_isready -h 127.0.0.1 -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']
interval: 10s
timeout: 5s
retries: 5
networks:
- deepfellow-infra-net
server:
env_file: .env
depends_on:
postgres:
condition: service_healthy
nightshift:
env_file: .env
depends_on:
postgres:
condition: service_healthy
volumes:
postgres:With this PostgreSQL service, set DF_POSTGRES_URL=postgres:5432 in .env.
Migrate Automatically at Server Start
Add the migration settings to .env, together with the PostgreSQL settings:
DF_DB_BACKEND=postgres
DF_MONGO_MIGRATION__ENABLED=true
DF_MONGO_MIGRATION__SNAPSHOT_DIR=/app/storage/mongo-snapshotsDF_MONGO_MIGRATION__ENABLED=true needs DF_DB_BACKEND=postgres. On MongoDB, the server, Nightshift, and the admin scripts refuse to start with it. For every migration setting, read Configuration.
Stop the server and Nightshift, and start them again with the new settings.
docker compose stop server nightshift
docker compose up -dThe server migrates the data before it starts serving. It logs each step: the MongoDB schema migrations, the snapshot, the copy, and the verification. On success, it logs The MongoDB data is migrated to PostgreSQL and verified with the path of the snapshot, and starts on PostgreSQL. Nightshift and the admin scripts don't touch PostgreSQL in the meantime: Nightshift waits and logs every minute that it waits, and the admin scripts, such as create_admin, refuse to run.
While the migration runs, the server's health check reports it as unhealthy. Docker Compose doesn't restart an unhealthy container, so a long migration is not interrupted. A service with depends_on: condition: service_healthy on the server starts only after the migration.
Once the migration finished, the settings do nothing. Remove the DF_MONGO_MIGRATION__* settings from .env.
If the Automatic Migration Refuses
If the migration refuses or fails, the server doesn't start. It exits with MongoDB to PostgreSQL migration refused, so the server does not start: and the reason. PostgreSQL never holds a verified migration in that case, and MongoDB stays unchanged, except for the pending MongoDB schema migrations that the first step applied. Each attempt deletes its own snapshot, so a server that restarts in a loop doesn't fill the disk.
| Reason in the log | What to do |
|---|---|
DF_MONGO_MIGRATION__ENABLED=true moves the data to PostgreSQL for a server that runs on it, so it needs DF_DB_BACKEND=postgres | Set DF_DB_BACKEND=postgres to migrate, or remove DF_MONGO_MIGRATION__ENABLED to stay on MongoDB. |
| No directory for the MongoDB snapshot | Set DF_MONGO_MIGRATION__SNAPSHOT_DIR. |
| MongoDB changed during or after the snapshot, so something still writes to it | Stop every container that uses MongoDB, then start the server again. |
| PostgreSQL has no migration marker but already holds rows | A server started on this PostgreSQL before the migration. To delete the rows and migrate, set DF_MONGO_MIGRATION__WIPE_TARGET=true for one start, then remove it. Or migrate to an empty database. |
| The migration found documents it could not migrate, or the verification found differences | Fix the documents in MongoDB, or set DF_MONGO_MIGRATION__SKIP_UNMAPPABLE=true to leave the unmappable documents out. |
MongoDB database NAME holds no collections | Check the DF_MONGO_* settings. A new install doesn't need the migration: remove DF_MONGO_MIGRATION__ENABLED. |
To retry, fix the cause and start the server again. To stay on MongoDB instead, set DF_DB_BACKEND=mongo and remove DF_MONGO_MIGRATION__ENABLED. If the migration was cut off, for example by a power loss, the next start empties PostgreSQL and migrates again. With DF_POSTGRES_ALLOW_INCOMPLETE_CUTOVER=true, the server may have written to that PostgreSQL, so the migration refuses instead. Set DF_MONGO_MIGRATION__WIPE_TARGET=true for one start to delete those writes and migrate.
Migrate with One Command
Instead of letting the server migrate at start, you can run the same step in a one-off container of the server image, and switch the server to PostgreSQL only afterwards.
Stop the server and Nightshift, and start MongoDB and PostgreSQL. Use the service names of your compose.yaml.
docker compose stop server nightshift
docker compose up -d db postgresRun the migration.
docker compose run --rm --no-deps server ./.venv/bin/python -m server.scripts.migrate_from_mongo \
--snapshot-dir /app/storage/mongo-snapshotsSet DF_DB_BACKEND=postgres in .env, and start the server and Nightshift.
docker compose up -d server nightshiftThe command exits with status 0 when the data is migrated, or when PostgreSQL already held the verified data. It exits with status 1 when it refuses or the migration found problems, and with status 2 when it cannot run at all, for example because a database is unreachable. It prints the same reasons as the automatic migration. The options override the DF_MONGO_MIGRATION__* settings:
| Option | Description |
|---|---|
--snapshot-dir <var>PATH</var> | Directory for the MongoDB snapshot. |
--skip-unmappable | Finish even if some documents do not map to the PostgreSQL schema. Those documents are left out. |
--wipe-target | Delete the rows of a PostgreSQL that has no migration marker before migrating. |
The command and a server with the automatic migration can run at the same time: one of them migrates, and the other waits and then finds the data migrated.
Roll Back Without DeepFellow CLI
The migration only reads MongoDB, apart from the pending MongoDB schema migrations applied before the snapshot. To go back, set DF_DB_BACKEND=mongo in .env, remove the DF_MONGO_MIGRATION__* settings, and restart the server and Nightshift. The server then uses the data that MongoDB had at the migration.
A rollback does not carry back anything written to PostgreSQL after the migration. Those writes stay in PostgreSQL only.
After a rollback, PostgreSQL still holds the verified migration, so the automatic migration and the one command do nothing on it. To move the MongoDB data again later, migrate to a new empty database.
We use cookies on our website. We use them to ensure proper functioning of the site and, if you agree, for purposes such as analytics, marketing, and targeting ads.
Vector Store Embedding Migration
Move every vector store in a project to a new embedding model or dimension, without losing data or blocking search.
Nightshift Background Jobs
The background worker that processes vector store files, Metadata Expansion, Document Summarization, Knowledge Graph, webhook, Fine-Tuning Pipelines, and migration jobs.