mongodb
postgresql

Migrate MongoDB to PostgreSQL

Beta — live-tested path

The converter maps 15 MongoDB column types to PostgreSQL. 5 of them change kind on the way (ObjectId becomes VARCHAR(24), Array becomes JSONB, Object / subdocument becomes JSONB, and more); the table below lists every one. Documents become rows: the schema is inferred by sampling the collection, and nested data lands in JSON columns. DBShifts creates the inferred tables, copies the documents in parallel batches and checks the result with row counts.

Start MongoDB → PostgreSQL Migration

Free tier · no credit card

MongoDB vs PostgreSQL

mongodb

MongoDB

the leading document database.

postgresql

PostgreSQL

the most advanced open-source relational database.

MongoDB to PostgreSQL data type mapping

Pulled directly from DBShifts's own conversion rules — not a general reference table, this is what actually runs.

MongoDBPostgreSQL
ObjectIdVARCHAR(24)
String (up to 255 chars)VARCHAR(255)
String (longer)TEXT
Int32INTEGER
Int64BIGINT
DoubleDOUBLE PRECISION
Decimal128NUMERIC(38,9)
BooleanBOOLEAN
DateTIMESTAMP
TimestampTIMESTAMPTZ
BinaryBYTEA
ArrayJSONB
Object / subdocumentJSONB
UUIDVARCHAR(36)
NullTEXT

Example: MongoDB to PostgreSQL conversion

MongoDB

// one document from the "orders" collection
{
  "_id":         ObjectId("6512c3a1f4b2e9d0a7c1b234"),
  "customer_id": NumberLong(4821),
  "total":       NumberDecimal("129.99"),
  "is_gift":     false,
  "notes":       "leave at reception",
  "tags":        ["priority", "fragile"],
  "placed_at":   ISODate("2026-03-14T09:20:00Z")
}

PostgreSQL

CREATE TABLE IF NOT EXISTS "orders" (
    "_id" VARCHAR(24),
    "customer_id" BIGINT NOT NULL,
    "total" NUMERIC(38,9) NOT NULL,
    "is_gift" BOOLEAN,
    "notes" VARCHAR(255),
    "tags" JSONB,
    "placed_at" TIMESTAMP NOT NULL,
    PRIMARY KEY ("_id")
);

Arrays and subdocuments become JSONB

Nested documents and arrays land in JSONB, PostgreSQL's binary JSON, with the data intact. That makes migrate-first, normalise-later realistic here in a way it is not on most targets: a GIN index on the column supports containment queries such as tags @> '["priority"]', and jsonb_array_elements() unnests an array into rows.

Split a field into a child table when it has to be joined, constrained or updated on its own. Until then, a JSONB column is a faithful copy of what the document held.

Decimal128 becomes NUMERIC(38,9)

Decimal128 maps to NUMERIC(38,9), exact, never through a float. The scale is fixed at nine digits, though, so a value with more fractional digits than that is rounded to nine, and the integer part can carry up to 29 digits.

Prices and quantities fit easily. Scientific values or exchange rates with long fractions should be checked first, and the column widened per field if any value exceeds nine decimal places.

BSON dates are UTC instants

A BSON Date is a point in time in UTC. It maps to TIMESTAMP without time zone, holding the UTC wall-clock value, so the data is correct as long as everything reading it knows it is UTC.

If PostgreSQL sessions run in another time zone, convert the column to timestamptz with ALTER COLUMN placed_at TYPE timestamptz USING placed_at AT TIME ZONE 'UTC'. PostgreSQL then shows each value in the session's zone without the instant changing.

MongoDB → PostgreSQL conversion notes

Engine-specific rules baked into the conversion and transfer pipeline — verified by live certification of this exact pair.

  • Schema is inferred by sampling documents — null values don't poison type inference (a nullable int stays an int column).
  • Decimal128 maps to exact DECIMAL on SQL targets, never a float round-trip.
  • Nested documents and arrays land as JSON/JSONB columns with data preserved byte-for-byte.
  • _id becomes the primary key; ObjectId converts to its 24-character hex form.
  • Bulk loads use sequence reseeding (setval to MAX) so SERIAL columns keep working after migration.
  • ENUMs from MySQL become native CREATE TYPE … AS ENUM — validation semantics preserved.
  • Foreign keys and indexes are applied after data load for dramatically faster transfers.

Frequently asked questions

Is MongoDB to PostgreSQL migration production-ready in DBShifts?

MongoDB to PostgreSQL is in beta. It runs on real servers in the certification suite, which checks it by row counts; the content checks that certify the SQL pairs do not yet compare documents with rows. Every migration still ends with a fidelity report to audit before cutover.

What happens to MongoDB types that PostgreSQL doesn't have?

Each gets the nearest PostgreSQL type the converter treats as safe: ObjectId becomes VARCHAR(24), Array becomes JSONB, Object / subdocument becomes JSONB, UUID becomes VARCHAR(36), Null becomes TEXT. Every such decision is in the migration plan before anything runs, and any mapping can be overridden per column.

Can I keep MongoDB and PostgreSQL in sync after the initial migration?

Yes, until you cut over. DBShifts follows a change stream from a resume token taken before the first document is copied. Change streams need a replica set or a sharded cluster; a standalone server cannot stream.

Which MongoDB data types need a second look when moving to PostgreSQL?

The table above carries a note on 5 of them: ObjectId (VARCHAR(24)), Decimal128 (NUMERIC(38,9)), Array (JSONB), Object / subdocument (JSONB) and more. Analysis flags the same columns on your own schema before any data moves.

Go deeper

Ready to move MongoDB to PostgreSQL?

Set up in under two minutes. Validation and rollback included.

Start Free Migration