A Node.js utility to migrate data from MongoDB to PostgreSQL for FeatBit v5.2.0.
Caution
Please do test migration with an empty PostgreSQL database first and always review the SQL scripts before executing them against production.
- Access to your FeatBit MongoDB database
- Access to your target PostgreSQL database
- FeatBit deployment upgraded to v5.2.0 before migration
-
Initialize your PostgreSQL database by running the
init-pg.sqlscript. This script will:- Create the
featbitdatabase. - Create all necessary tables and indexes.
- Create the
-
Clone this repository to your local machine and install dependencies:
git clone <repository-url> cd featbit-db-migrator npm install
-
Configure Connection Strings:
- Create a
.envfile in the root directory based on the.env.templatefile. - Fill in your MongoDB and PostgreSQL connection strings in the
.envfile.
- Create a
Connection String Formats:
- MongoDB: Follow the MongoDB connection string format.
- PostgreSQL: Follow the Postgres connection string format.
-
Generate CSV Files
Run the following command to extract data from MongoDB and generate CSV files:
node index.js
This will:
- Connect to both MongoDB and PostgreSQL
- Extract data from MongoDB collections
- Filter out records that already exist in PostgreSQL
- Generate CSV files for each collection in the
csvs/directory
-
Generate Import Scripts
Run the following command to generate SQL import scripts:
node get-scripts.js
This will create a file named
scripts-to-run.sqlcontaining\copycommands to import each CSV file into the corresponding PostgreSQL table. -
Import Data to PostgreSQL
For better progress monitoring and error handling, we recommend executing commands one at a time:
psql -U your_username -d featbit # Then run each \copy command from scripts-to-run.sql individually
Note
The following collections are not migrated by this tool:
EndUsers- End user dataEvents- Analytics and insights data
These collections typically contain large volumes of data and may require separate migration strategies.
- Verify connection strings are correct
- Check network access to both databases
- Ensure MongoDB user has read permissions
- Ensure PostgreSQL user has write permissions
- Check PostgreSQL logs for detailed error messages
- Verify CSV files in
csvs/directory are not empty - Ensure file paths in
scripts-to-run.sqlare absolute paths - Check for encoding issues (UTF-8 is required)
If you encounter issues not covered here, please open an issue in this repository or join our Slack community for assistance.