How to move data from PostgreSQL to MongoDB
The process for transferring data from PostgreSQL to MongoDB is clear-cut. Ultimately, the ease of your task depends on the complexity of the PostgreSQL database and the structure of document collections needed in the new MongoDB database.
To migrate data, you’ll extract it from PostgreSQL and then import it to MongoDB using the mongoimport tool. Let’s look at the two different ways to extract the data: returning queries as tab-separated values (TSV) or as JSON.
Using TSV to transfer data
The TSV format is the quick and easy option. However, it only works if the original PostgreSQL schema is relatively simple and you don't need to embed documents in other documents with a one-to-many relationship.
For example, if you need to create a collection of documents that represent customers and plan on embedding each customer's order in these documents, TSV won’t work because it doesn’t support hierarchical data structures. Each row in the TSV will become a document. You can still map values in each row to fields deeper in your documents; you just can’t embed documents.
You could create an address field and create nested state and city fields as in the example below. However, you could not store multiple address entities. Let's look at an example query to see how this works.
Consider the PostgreSQL created table “users.”
Notice that we renamed each column we are exporting with the AS command. The format of the alias being created is mongoFieldName.type(). For example, we have userId.int32(). When we execute the import, mongoimport will parse this header and create the fields with the correct types. On most of the columns, we use the auto() type and let mongoimport determine the type based on context.
For the upgraded column, which is a boolean, PostgreSQL will return a t for true and f for false. This default value won't be recognized by MongoDB, so we use a CASE statement to set values that will work.
You can also do some limited data nesting using the TSV migration process. The city and state fields are an example.
The final line in the query formats the export as CSV with a header using a tab delimiter, which is what makes the file TSV format. We use this command to import the file into MongoDB Atlas:
This will result in the following document in the “users” collection:
Using JSON to transfer data
Using JSON for data migration is preferable if your PostgreSQL schema is complex and you want to nest arrays of related records inside of a MongoDB document.
To return the results of a PostgreSQL query as JSON, you will need three functions:
- row_to_json: Returns a row as a JSON object with column names as keys and the values from the row
- array_to_json: Returns an array of data as a JSON array
- array_agg: Accepts a set of values and returns an array where each value in the set becomes an element in the array
Let’s look at an orders table which in our relational schema keeps a record for every product ordered by the user:
Here is an example query using all three functions:
The query above will create a file orders.json with JSON documents for each user from the users table:
Once you have written the query and saved it, you can use mongoimport to import the file: