-
Notifications
You must be signed in to change notification settings - Fork 5
Migrating between MongoDB and PostgreSQL
This page covers the built-in one-time remote database migration flow in ytdl-material.
Use this when you want to move:
- MongoDB -> PostgreSQL
- PostgreSQL -> MongoDB
If you are moving from the local JSON DB to a remote DB, do not use ytdl_db_migrate. See Configuration and Environment Variables instead.
On startup, ytdl-material can copy all of its data from one remote database to the other before the app starts normally.
-
ytdl_db_migrate=postgresmeans MongoDB -> PostgreSQL -
ytdl_db_migrate=mongomeans PostgreSQL -> MongoDB
The source DB is left untouched if the migration fails.
Before starting a remote-to-remote migration, all of the following must be true:
-
ytdl_use_local_dbmust befalse -
ytdl_mongodb_connection_stringmust be set -
ytdl_postgresdb_connection_stringmust be set -
ytdl_db_migratemust be set to eitherpostgresormongo - the target database must be empty
If the target database already contains data, startup will fail instead of merging or overwriting existing records.
Set:
ytdl_use_local_db: 'false'
ytdl_mongodb_connection_string: 'mongodb://ytdl-mongo-db:27017'
ytdl_postgresdb_connection_string: 'postgresql://ytdl-material:ytdl-material@ytdl-postgres-db:5432/ytdl-material'
ytdl_db_migrate: 'postgres'Then start the app.
On a successful startup migration:
- data is copied from MongoDB to PostgreSQL
- PostgreSQL becomes the saved remote DB type
- the saved
ytdl_db_migratesetting is cleared automatically
After the migration succeeds, remove the ytdl_db_migrate environment variable from your container/compose config so it is not applied again on the next boot.
Set:
ytdl_use_local_db: 'false'
ytdl_mongodb_connection_string: 'mongodb://ytdl-mongo-db:27017'
ytdl_postgresdb_connection_string: 'postgresql://ytdl-material:ytdl-material@ytdl-postgres-db:5432/ytdl-material'
ytdl_db_migrate: 'mongo'Then start the app.
On a successful startup migration:
- data is copied from PostgreSQL to MongoDB
- MongoDB becomes the saved remote DB type
- the saved
ytdl_db_migratesetting is cleared automatically
After the migration succeeds, remove the ytdl_db_migrate environment variable from your container/compose config so it is not applied again on the next boot.
This migration is intentionally strict.
- it will not merge data from both databases
- it will not append into a populated target
- it will abort startup if the target already has data
If startup fails and you leave ytdl_db_migrate in the environment, the container will try the migration again on every restart.
That is expected. Remove or fix the migration env vars before restarting if you do not want another migration attempt.
For local JSON -> remote DB, set ytdl_use_local_db=false and provide the remote connection string.
If the configured remote DB is empty and the local DB has data, ytdl-material will automatically copy the local DB into that remote DB on startup.
Do not set ytdl_db_migrate for local JSON -> remote moves.
If you previously tried a migration with an older build and the PostgreSQL target was already created, it may be safest to recreate the target PostgreSQL database or schema before retrying.
For example:
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;Or remove and recreate the PostgreSQL volume/container if that is how you manage it.
Do not wipe the source database unless you have verified the migration succeeded and the app is running against the new target.