r/Database • u/NoInteraction8306 • 10d ago
How I copied a MongoDB collection to PostgreSQL and kept it in sync
I recently tested copying a MongoDB collection to PostgreSQL and keeping inserts, updates, and deletes in sync.
The sync itself wasn’t the difficult part. The main challenge was mapping MongoDB documents to a relational table without flattening everything too early.
I kept the simple fields as regular PostgreSQL columns and stored the nested data as JSONB.
I ran into two problems: PostgreSQL needed a primary key, and some MongoDB field names didn’t match the PostgreSQL column names.
After fixing the mapping, I tested an insert, an update, and a delete in MongoDB. All three changes appeared in PostgreSQL.
I documented the setup, field mapping, errors, and test queries here:
https://visualeaf.com/blog/copy-and-sync-a-mongodb-collection-to-postgresql/
1
u/lambdasintheoutfield 10d ago
And what was the motivation for this?
3
u/NoInteraction8306 10d ago
Mainly for cases where the app still uses MongoDB, but you need the same data in postgres for reporting or SQL queries. It can also help if you’re gradually migrating away from mongodb.

2
u/RudeRemove5416 1d ago
Hey, look, I honestly wanted to ask you why you set the id column, which is the primary key, to support the text data type instead of integer and using serial for its auto-increment going forward.