In one of our previous articles, we compared and discussed the differences and similarities between the database management systems MariaDB and MySQL. These database management systems are two of the most popular relational databases currently available, but they're not the only ones on the market.

So, in order to explore the other database options that might be more suited to your business, we're now going to compare MongoDB vs PostgreSQL.

However, in order to explain the differences between MongoDB vs PostgreSQL, we must first explain the differences between SQL and NoSQL databases.

SQL vs NoSQL databases

SQL stands for Structured Query Language and is used for defining and manipulating data inside of a database. Thus, the main difference between SQL and NoSQL databases is the way in which they structure data.

An SQL (relational) database will store data in tables made up of columns and rows, whereas a NoSQL (non-relational) database stores data via the use of documents.

To provide an example, take the basic structure of a social media site and its core elements: the ability to post, host users, leave comments,  and like content. In an SQL database, the data for these elements would be stored in individual tables. These tables are all linked, so to see which ‘user’ ‘liked’ which ‘post’ you use what's known as a JOIN clauses to collate related data between the tables and bring it all together in a new table.

For NoSQL databases however, the social media site would have to create a document for each post. Each document would hold non-relational data about that specific post, and therefor show within the document who the user is, which users liked the post, which users commented on the post, and what the comment said. A NoSQL ‘document’ for a post would likely be written in JSON (JavaScript object notation) and might look something like this:

{    "POST": "62789739812793",    "Author": ["John Doe", "UID4"],    "Likes": {         "Count": 5,         "Users": ["UID2435","UID7494", "UID8999", "UID9000", "UID9432"]    },    "Comments": {         "CommentId_1": {                "UserID": "UID1",                "UserName": "Daisy Dukes",                "Comment": "Lorem ipsum dolor!",                "Timestamp": "Sat, 29 Nov 2014 16:57:56 GMT"          }    } }

Advantages of SQL vs NoSQL

In short, NoSQL databases tend to offer more flexibility in how they collect and display data. Continuing with our previous example, if one of the comments on a post includes a photo, or an added description, all you would have to do in a NoSQL database is add a new field to the document in question to cover the new data type.

However, if you wanted to do the same thing in an SQL database, you would need to reformat and restructure the database to allow for the new fields. This requires locking down the tables until the data fields are updated, which can lead to a potentially lengthy period of downtime.

On a broader scale, NoSQL databases are better for data that has relatively little structure, but will also have new types of data added on a regular basis. This allows you to freely upload new data types without having to know what ‘type’ the data will be when you first set up the database. This ultimately saves you time up front when when you set up the database.

That being said, if you know that your data structure will always remain the same, then an SQL database will likely be a better option. The schema of an SQL database minimises the chances of data loss and stops data tables from getting out of sync. Along with its improved ability for conforming with ACID compliance (atomicity, consistency, isolation, and durability), SQL databases wind up being the better choice for businesses that use static databases

What is PostgreSQL?

Now that we understand the differences between NoSQL and SQL databases, it's time to answer the question, 'what is PostgreSQL?'

Simply put, PostgreSQL is one of the most advanced object-relation database systems presently in use, having been around in some form since 1997. It's now typically used for a variety of research and production applications; predominantly in financial, medical, and geographic-based information systems.

PostgreSQL is so popular that it runs on all major operating systems, has been ACID-compliant since 2001, and has a range of powerful add-ons that give it excellent adaptability.

Why use PostgreSQL?

The primary aim of PostgreSQL is to help developers build various applications, protect data integrity, and easily manage any sized database through its suite of features and tools. As database software goes, PostgreSQL is highly customizable, even allowing you to code in different programming languages without rebuilding your database from scratch.

What is MongoDB?

Thanks to our previous definitions, we don't have to worry about asking 'how does MongoDB work?' Unlike PostgreSQL, MongoDB is a NoSQL database management programme, meaning it uses a series of JSON-like documents to build its databases.

First released in 2007, MongoDB has gone on to be one of the most popular NoSQL database options for those working with large levels of data. MongoDB is often the preferred option for customer service applications, data archiving, and is even used by CERN for data aggregation and discovery.

Why use MongoDB?

MongoDB uses a variety of document records which are made up of data composed of field and value pairs. These are similar to typical NoSQL JSON documents but use a binary equivalent. Thus, these BSON documents allow MongoDB to accommodate more data types while mimicking the column system of a relational database for improved clarity and readability.

These documents can even contain other documents within them, meaning that a wealth of data can be contained inside one single document for ease of storage.

MongoDB vs PostgreSQL

Being SQL and NoSQL database systems, the advantages and disadvantages of MongoDB and PostgreSQL tend to fall into areas related to their base system capabilities. With that being said, there are nuances between the two that help them to stand out from other similar database systems.

Advantages of PostgreSQL

In general, PostgreSQL is highly expandable, and as mentioned previously, has a range of application extensions available for use thanks to its open-source nature. It's also largely compliant with current SQL standards but is still flexible enough to overcome known SQL issues.

On top of this, while MongoDB naturally uses a form of JSON as a NoSQL database, PostgreSQL displays its unique qualities from other relational databases by also supporting JSON data. This makes PostgreSQL a viable alternative to MongoDB for those looking to store static JSON data types while still retaining the structured nature of an SQL database.

PostgreSQL is also fully ACID compliant, which ensures that data transactions are secured for a wide range of scenarios. For example, in a bank transaction, you want to make sure that all your records remain in sync with each other, and in the event of a power outage or disaster, have transactions revert back to their original state so no money is lost or duplicated. PostgreSQL is able to easily accommodate this thanks to its ACID compliant design.

Finally, PostgreSQL has a wider base of community support. Being open-source software, customer support is most predicated on the community knowledge available. Therefore, PostgreSQL has a distinct advantage over MongoDB when it comes to support as it's been around for 12 years longer.

Disadvantages of PostgreSQL

The primary disadvantages of PostgreSQL come down to the restrictions place don it by its SQL nature/ PostgreSQL is not suited towards flexible nature, even with its JSON compatibility. It also takes far longer to update a PostgreSQL database due to its writing time, meaning it can be slow to pull and update tables.

While PostgreSQL does offer the ability for horizontal scaling through a partitioning technique known as 'sharding', this is much more complex to execute due to PostgreSQL's restrictive structure.

Lastly, while highly adaptive in many ways, PostgreSQL is actually not supported by many opensource apps, especially those already supporting MySQL.

Advantages of MongoDB

As previously discussed, a NoSQL document-orientated structure like MongoDB is far better suited for handling new data types and introducing them into a database system than an SQL database. MongoDB also doesn't require a predefined schema to act as its framework and is able to store any type of data presented to it; its documents are fully editable at a moments notice.

MongoDB can also work with multiple programming languages and tends to have good third party support for enhanced storage options. Its embedded document qualities also means there's a reduced need for database joins, speeding up the database as a whole.

And one of the other final advantages of MongoDB is its native scaling abilities. MongoDB offers out-of-the-box support for scaling a database over multiple servers. As more and more data is stored in the database, MongoDB is capable of scaling elastically and horizontally to encompass all new editions.

Disadvantages of MongoDB

Of course MongoDB is not without its disadvantages. one of the primary disadvantages of MongoDB is that it's not ACID compliant. This immediately makes MongoDB less suited for databases that require flawless security and redundancy in their functions.

MongoDB also has a limited data writing speed when having to tackle large volumes of data simultaneously thanks to having only a single master code. This can also slow continuity to a crawl in the event of the master code failing, requiring the system to take a minute a minute or more to switch to a new master code and reboot.

Lastly, MongoDB's relatively young lifespan means that there is limited community support currently available for its use.

How to install MongoDB or PostgreSQL

For those interested in how to start MongoDB or how to install PostgreSQL, the installation process for both is very simple.

For PostgreSQL, you need only download the appropriate installation portal from the official PostgreSQL website, ensuring that it is compatible with whatever operating system you use.

As for MongoDB, it too can be installed using the latest editions found on the MongoDB website. However, there is now a MongoDB service that allows you to operate MongoDB on the cloud without installing it directly.

Regardless of which system you choose, there are a variety of in-depth and easy to follow tutorials online that can walk you through both the installation process step-by-step, and teach you how to use PostgreSQL and MongoDB even in you're a beginner.

As is often the conclusion when comparing these types of systems, there is no overall best choice for a database management system. Instead, your choice will relate entirely to your situation, database requirements, and the project you'll be using it with. But no matter which database type you choose for your projects, here at Fasthosts we support both PostgreSQL and MongoDB on our CloudNX platform.

If you're interested in what other services we can provide, Fasthosts also offers domain hosting options, cloud and dedicated servers, and WordPress solutions for all your online needs. And if you want to build on your knowledge of websites and the internet in general, you can read plenty of articles and guides on the Fasthosts blog.