MongoDB is a popular open-source NoSQL database that stores data in a flexible, document-based format. It is designed to handle large volumes of unstructured data and is highly scalable, making it a popular choice for modern web applications. MongoDB uses a JSON-like format to store data, which allows for easy integration with web applications and APIs. It also supports dynamic queries, indexing, and aggregation, making it a powerful tool for data analysis. MongoDB is widely used in industries such as finance, healthcare, and e-commerce, and is known for its ease of use and flexibility.
An object-relational database management system, PostgreSQL is able to handle a wide range of workloads, supports multiple standards, and is cross-platform, running on numerous operating systems including Microsoft Windows, Solaris, Linux, and FreeBSD. It is highly extensible, and supports more than 12 procedural languages, Spatial data support, Gin and GIST Indexes, and more. Many web, mobile, and analytics applications use PostgreSQL as the primary data warehouse or data store.
1. First, you need to have a MongoDB instance running and accessible from the internet. You will also need to have the necessary credentials to access the database.
2. In the Airbyte dashboard, click on "Sources" and then click on "New Source."
3. Select "MongoDB" from the list of available sources.
4. In the "Connection Configuration" section, enter the following information:
- Host: The hostname or IP address of your MongoDB instance.
- Port: The port number on which your MongoDB instance is running.
- Username: The username you use to access your MongoDB instance.
- Password: The password you use to access your MongoDB instance.
- Authentication Database: The name of the database where your authentication credentials are stored.
5. Click on "Test Connection" to ensure that Airbyte can connect to your MongoDB instance.
6. If the connection is successful, click on "Save" to save your MongoDB source configuration.
7. You can now create a new pipeline and select your MongoDB source as the input. You can then configure the pipeline to transform and load your data into your desired destination.
With Airbyte, creating data pipelines take minutes, and the data integration possibilities are endless. Airbyte supports the largest catalog of API tools, databases, and files, among other sources. Airbyte's connectors are open-source, so you can add any custom objects to the connector, or even build a new connector from scratch without any local dev environment or any data engineer within 10 minutes with the no-code connector builder.
We look forward to seeing you make use of it! We invite you to join the conversation on our community Slack Channel, or sign up for our newsletter. You should also check out other Airbyte tutorials, and Airbyte’s content hub!
MongoDB is a distributed database that is built for modern transactional and analytical applications and may be used for rapidly changing, multi-structured data. On the other hand, PostgreSQL is an SQL database that has all of the features that you require from a relational database . If you are unsure of the differences between these systems, on the MongoDB website, you can find an article that compares PostgreSQL and MongoDB.
Choosing one or the other between MongoDB and PostgreSQL may not be your only option – in-fact, because each database has different strengths you may wish to use them side-by-side. If this is your case, then you may need to sync data between them.
Custom building a data pipeline to replicate data from MongoDB to Postgres is time-consuming and tedious. On the other hand, Airbyte is designed exactly for this task. This article will demonstrate how to use Airbyte to replicate and synchronize data from MongoDB to PostgreSQL!
This tutorial makes use of the following tools:
- Clever Cloud - hosting of the MongoDB and PostgreSQL databases.
- mongosh - a CLI client to interface with the MongoDB database.
- Postgres CLI - a terminal-based front-end to PostgreSQL.
- Airbyte cloud - a data integration tool that will be used to replicate and synchronize data between MongoDB and PostgreSQL. Alternatively, you may choose to install Airbyte OSS locally.
Step 1-a: Launch MongoDB
In this section, you will use Clever Cloud to create a MongoDB instance. Once you sign up, choose the option to create an add-on from your personal space.
From the available list of add-ons, choose the MongoDB add-on.
For the instance size, choose the DEV plan, which is free to use.
Enter an add-on name and select a region as shown below, and then click Next.
You should now have a new MongoDB database created with all the details to connect to it. Copy the mongo CLI connection command from the Clever Cloud add-on dashboard shown below
Copy and replace “mongo” with “mongosh” before executing the command in your terminal, as demonstrated below:
You should now be connected to the PRIMARY replica of the MongoDB replica set (indicated by [primary] in the shell).
In the Airbyte connection to MongoDB, you will make use of the URL for the primary replica. This can be retrieved by running rs.isMaster().primary in the MongoDB shell, which will respond with a string in the format of [hostname]:[port]. In our case, the URL returned by this command is is n2-c2-mongodb-clevercloud-customers.services.clever-cloud.com:27017
Step 1-b: Add data to MongoDB (optional)
mongoimport --host=n2-c2-mongodb-clevercloud-customers.services.clever-cloud.com --port=27017 --username= --password= --db= –collection=restaurant restaurant.json
Step 2: Configure a MongoDB source connector
Create an Airbyte MongoDB source by choosing sources from your Airbyte dashboard and clicking on the New source button. Then from the list of sources, choose MongoDB, and you should see a UI similar to the following:
To keep this tutorial simple, and for demonstration purposes only, in the above image we have selected a Standalone MongoDB instance. However, you may also consider selecting one of the alternative MongoDB configuration parameters if you wish to have a more resilient connection to your MongoDB cluster.
Enter the Host, Port, Username, DB Name, and Password that were shown earlier in the Clever Cloud MongoDB configuration UI. Then choose Set up source.
Step 3: Launch PostgreSQL
To set up a PostgreSQL database, create a new add-on on your Clever Cloud dashboard, and choose PostgreSQL from the available add-ons
For plan, choose the DEV option, which provides 256 MB for storage.
Give a name to your add-on, and choose a location.
Click on Next once you are satisfied with your configurations, and then Clever Cloud should show you the PostgreSQL database credentials with information that will be required by Airbyte, including host, user, password, and database name.
To connect to our newly created database, copy the Connection URI and provide it as an argument to the psql CLI tool as shown below.
Step 4: Configure a PostgreSQL destination connector
Go to Destinations in your Airbyte Dashboard, choose to Create destination from the list, and choose PostgreSQL. You will then see a UI similar to the following:
Enter in the PostgreSQL parameters that were returned by Clever Cloud, and click Set up destination.
Step 5: Set up an Airbyte connection from MongoDB to Postgres
Go to Connections in your Airbyte dashboard and choose New connection. Select the source and the destination that you just created, at which point you should see a UI similar to the following:
Airbyte has correctly detected the restaurant collection as a stream, and you can choose how it should be replicated to PostgreSQL. For sync mode, choose one from the available modes – for more information you may wish to consult the blog: An overview of Airbyte’s replication modes.
For Replication frequency, specify the interval between sync runs. Once you are done with the configurations, choose Set up connection and Airbyte will start its first sync. Once complete, you will be able to see how many records were replicated.
Log in to Postgres host to see the replicated data. Note that you must change the search_path according to the DB Name that you specified when you set up the PostgreSQL destination in Airbyte.
You should now be able to view the replicated data using standard SQL commands.
In summary, in this tutorial you have learned how to:
- Launch a MongoDB database on Clever Cloud, and then how to add some test data to it.
- Configure an Airbyte source connector to read data from MongoDB.
- Launch a PostgreSQL database on Clever Cloud.
- Configure an Airbyte destination connector to send data into PostgreSQL.
- Create an Airbyte connection that replicates data from a MongoDB to Postgres.
With Airbyte, the data integration possibilities are endless, and we look forward to seeing you use it! We invite you to join the conversation on our community Slack Channel, participate in discussions on Airbyte’s discourse, or sign up for our newsletter. You may also be interested in other Airbyte tutorials and Airbyte’s blog!