MongoDB (Atlas) to PostgreSQL migration: mapping, tools and cutover
How to move a MongoDB or Atlas database to PostgreSQL: decide the schema, copy the data, keep it in sync with change streams, switch over, and know when to stay.
45 minutes with an engineer: we review your workload and sketch the design.
- ✓Schema mapping
- ✓Change streams
- ✓JSONB
To migrate MongoDB Atlas to PostgreSQL, first decide per collection whether documents become relational tables or stay as JSONB. Copy the data with mongoexport or an application script, keep it current by reading MongoDB change streams, verify counts and samples, then switch writes. No common tool does the whole move for you, so the schema work is yours (checked 5 Dec 2026).
Customers
Partners
Should documents become JSONB or relational tables?
Use relational columns for fields you filter, join or constrain, and JSONB for the parts that really vary from document to document. A common result is a table with typed columns plus one JSONB column. PostgreSQL's jsonb type can be indexed and queried, so the choice can be made collection by collection (PostgreSQL docs, checked 5 Dec 2026).
| Approach | Best when | Cost |
|---|---|---|
| One JSONB column per collection (id plus doc) | You want the fastest move and the application keeps reading whole documents | You keep the schema problems and lose some of what PostgreSQL offers |
| Typed columns plus a JSONB column for the leftovers | A core set of fields is stable and queried; the rest varies | You write and maintain the mapping |
| Fully relational with child tables | Embedded arrays of objects are queried, joined or aggregated, and integrity matters | The most work, the biggest long-term gain |
Our guidance for choosing a target shape; the jsonb behaviour it relies on is cited below.
The PostgreSQL manual lists the jsonb behaviours that matter for a port: it stores a decomposed binary form, does not keep key order or whitespace, keeps only the last value for a duplicate key, and supports GIN indexes. A GIN index with the jsonb_path_ops class is usually much smaller and faster for containment (@>) searches, but cannot answer key-exists questions (PostgreSQL docs, checked 5 Dec 2026). The manual also warns that any update locks the whole row, so it recommends keeping JSON documents to a manageable size. Large, frequently updated documents are the first place a JSONB-only design hurts.
How do MongoDB types and structures map to PostgreSQL?
Most fields map directly. The decisions are ObjectId, dates, decimals, arrays and references. Settle them once, write the rules down, and apply them in one conversion step so every collection follows the same mapping.
| MongoDB | PostgreSQL option | Note |
|---|---|---|
| _id as ObjectId | text of 24 hex characters, bytea or uuid | Keep the original value; do not regenerate ids. It becomes the upsert key during sync. |
| String, int, long, double, bool | text, integer, bigint, double precision, boolean | Direct. |
| Decimal128 | numeric | Do not pass money through double precision. |
| Date | timestamptz | Stored as UTC in MongoDB; keep it that way. |
| Embedded object | Columns, or a jsonb column | Flatten if its fields are queried. |
| Array of scalars | An array column or jsonb | A GIN index covers membership searches. |
| Array of objects | A child table with a foreign key | Where the relational gain usually comes from. |
| Reference to another _id | Foreign key | Existing orphans will surface as errors: decide how to handle them. |
| Missing field versus null | NULL, or a default | Decide whether the two mean different things. |
Mapping choices are our recommendations. The export format is documented in the mongoexport and Extended JSON docs.
One trap is the export format. mongoexport writes JSON in MongoDB Extended JSON v2, relaxed mode by default, with canonical mode as an option. A date comes out as {"$date": ...} and an ObjectId as {"$oid": ...}, so a conversion step must unwrap them. CSV output needs an explicit field list and is a poor fit for nested data (mongoexport docs, checked 5 Dec 2026).
Which tools move data from MongoDB to PostgreSQL?
There is no single supported one-click converter from MongoDB to PostgreSQL. You assemble the move from documented parts: mongoexport or a driver script for the initial copy, change streams or the Debezium MongoDB connector for ongoing changes, and a small conversion program you write for the mapping.
| Piece | What it does | Limits to know | Source |
|---|---|---|---|
| mongoexport | Writes a collection as JSON or CSV, with an optional query | Reads from the primary by default; the docs say it is not a backup tool | docs |
| Application or driver script | Reads documents in batches, applies your mapping, inserts into PostgreSQL | You write and test it; usually the most flexible option | Your code |
| Change streams | Real-time feed of inserts, updates, replaces and deletes | Replica sets and sharded clusters only; resuming needs enough oplog history | docs |
| Snapshot, then change-stream events with operation codes c, u, d and r | Standalone servers unsupported; update events carry the full document only in a specific capture mode | docs | |
| FerretDB | A proxy that speaks the MongoDB protocol and stores data in PostgreSQL | A way to keep the application's MongoDB driver calls; see below | repo |
Sources linked in each row; checked 5 Dec 2026.
Debezium's MongoDB connector uses change streams, takes an initial snapshot when it first starts, and is documented for replica sets and for sharded clusters where each shard is a replica set. Its docs list 3.7 as the latest stable version and describe it running in Kafka Connect, so it brings that moving part along with it (Debezium docs, checked 5 Dec 2026).
FerretDB is a different idea: its repository describes an Apache-2.0 proxy that converts MongoDB wire-protocol queries to SQL on PostgreSQL with the DocumentDB extension, positioned as a drop-in replacement for MongoDB 5.0 and later in many cases (FerretDB repo, checked 5 Dec 2026). It can shorten the application rewrite, but your application then depends on its compatibility, so test the exact queries and aggregation stages you use.
What are the steps of a MongoDB to PostgreSQL migration?
Inventory the collections and queries, design the PostgreSQL schema, build and rehearse the conversion, load a snapshot while recording the change-stream position, apply changes until the two sides match, verify, then switch writes. Keep MongoDB intact as the fallback until PostgreSQL has run normally.
Inventory
List collections, sizes, indexes and, more importantly, every query and aggregation the application runs. The queries decide the schema.
Design
Choose the shape per collection from the table above, write the type rules, and create the schema in a test PostgreSQL database.
Convert and load
Open a change stream first to record its resume token, then copy the existing data through your mapping. Starting the stream first means nothing falls into the gap.
Apply changes
Read the stream and upsert by _id: insert or update on conflict, and delete on a delete event. Applying the same event twice must be harmless.
Verify
Compare counts per collection, then field-level samples and sums. Run the application's test suite and its slowest queries against PostgreSQL.
Cut over
Pause writes briefly, let the stream drain, switch the application, and watch. MongoDB stays untouched as the fallback.
Three change-stream details shape the build. Resuming needs enough oplog history to find the token, so size the oplog for the longest copy. A delete event has no document body, and fullDocument lookups on updates can come back empty if the document was deleted meanwhile. The docs recommend pre- and post-images, available from MongoDB 6.0, over lookups when you need the before and after state (change streams docs, checked 5 Dec 2026). Each change event must stay under 16 MiB. Replica set mechanics are covered in MongoDB replica sets explained.
How does the cutover work, and can you roll back?
The switch is a short write pause: drain the stream, confirm the counts, point the application at PostgreSQL. Rolling back is easy only if you decide the rule in advance, because none of these tools writes changes from PostgreSQL back into MongoDB.
- The stream is caught up and verification passes on a recent sample.
- The application stops writing to MongoDB, the last events apply, and sequences or counters are set.
- The application connects to PostgreSQL and a smoke test runs.
- MongoDB stays read-only for an agreed period as the fallback.
- Before the switch, decide what happens to writes made on PostgreSQL if you roll back. Without a reverse sync, they are lost or replayed by hand.
This is where a rehearsal matters. Run the whole sequence once against a copy, with the application, and time the write pause. That number, not a guess, goes into the change window.
When should you not move from MongoDB to PostgreSQL?
Stay on MongoDB when the application leans on features PostgreSQL does not replicate, when the data is truly schema-flexible, or when the real problem is the bill rather than the engine. A move costs a schema design, an application rewrite and a verification effort. The saving has to be worth that.
Contact us if
- ✓Your data is mostly relational already: references between collections, repeated joins done in application code.
- ✓You need transactions, constraints and SQL reporting across what are now separate collections.
- ✓You want one engine for documents and tables, with JSONB covering the flexible parts.
Not a fit if
- ✕The application depends on aggregation-pipeline features, Atlas Search or vector search that you would have to rebuild.
- ✕Collections are large, sharded and written heavily, and nobody has tested PostgreSQL at that shape.
- ✕The motive is Atlas pricing. Running MongoDB yourself or via a managed provider may close the gap with far less change: see moving off Atlas without downtime and Atlas pricing.
For the engine-level trade-offs, read MongoDB vs PostgreSQL. If you decide to stay, managed MongoDB is run by OSStend engineers on community open-source MongoDB; if you move, managed PostgreSQL is the target, in OSStend's cloud, your cloud account or your data center, with the design signed off first.
Questions about MongoDB Atlas to PostgreSQL
How do I migrate from MongoDB Atlas to PostgreSQL?
Design the target schema, copy the data with mongoexport or a script that applies your mapping, keep it current by reading change streams, verify, and switch writes. Atlas clusters are replica sets or sharded clusters, which is what change streams need (MongoDB docs, checked 5 Dec 2026).
Can PostgreSQL store MongoDB documents?
Yes, in a jsonb column, which can be indexed with GIN and queried with operators and jsonpath. Key order and whitespace are not preserved (PostgreSQL docs, checked 5 Dec 2026).
Is there a tool that converts a MongoDB schema to PostgreSQL automatically?
We found no maintained, vendor-documented tool that does the whole conversion. The pieces (export, change streams, Debezium) are documented; the mapping is code you write.
Can I keep my MongoDB driver code and use PostgreSQL?
FerretDB aims at that by proxying the MongoDB protocol onto PostgreSQL. Test your own queries first: compatibility is described as covering many cases, not all (FerretDB repo, checked 5 Dec 2026).
How do I keep data in sync during the migration?
Open a change stream before the initial copy and apply its events by _id. Debezium's MongoDB connector does the same inside Kafka Connect (Debezium docs, checked 5 Dec 2026).
Related pages
- Learn · MongoDB operationsMongoDB replica set: elections, failover, what breaksHow a MongoDB replica set works in production: elections, failover time, write concern, rollbacks and oplog size, from the MongoDB manual. Docs read 4 Dec 2026.
- Migration guideMigrate MongoDB off Atlas without downtimeMigrate MongoDB off Atlas with mongosync or mongodump: version limits, what to check, how cutover works, and managed running by OSStend engineers.
- ComparisonMongoDB Atlas pricing explained (2026)MongoDB Atlas pricing from MongoDB's own page: Flex and M10 to M80 tiers, backup, transfer and uplifts, plus a self-run cost comparison. Checked 4 Dec 2026.
- ComparisonMongoDB vs PostgreSQL for production workloadsMongoDB vs PostgreSQL for production workloads: data model, transactions, scaling and licence compared, with sources and dates, from a team that runs both.
- MongoDBMongoDB hosting: managed replica sets, shardingMongoDB hosting run 24/7 by OSStend engineers: replica sets and sharded clusters with HA, backups, point-in-time recovery, monitoring and upgrades.
- PostgreSQLManaged PostgreSQL service and Postgres hostingManaged PostgreSQL service, compared: how to choose between RDS, Aiven, Crunchy Bridge, Neon and a tailored cluster run 24/7 in your cloud or ours.
Plan your MongoDB move with an engineer
45 minutes with an engineer: the current setup is reviewed, the method is chosen and the cutover is sketched. Designs are written and signed off first, and a failover is rehearsed before go-live. Pricing is quoted per design.
- ✓A design sketch for your workload, not a plan menu
- ✓A zero-downtime cutover path for what you run today
- ✓A price for that design, after the review





