Difference between PostgreSQL and MongoDB

1. PostgreSQL (Object-Relational Database) :
PostgreSQL is a powerful, open source object-relational database management system (ORDBMS) with an emphasis on extensibility and standards compliance that uses and extends the SQL language combined with many features that safely store and scale the most complicated data workloads. PostgreSQL is ACID-compliant, transactional, that stores the data in the tabular format and uses constraints, triggers, roles, stored procedures and views as the core components.

Why use PostgreSQL ?

  • Free and open source.
  • Available in multiple languages.
  • Highly Extensible.
  • Protects data integrity.
  • Builds fault-tolerant environments.
  • Robust access-control system
  • Supports international characters.
  • Apple uses PostgreSQL!

Writing queries in PostgreSQL:

  • Creating students table
     CREATE TABLE students (id INT, name VARCHAR (100)); 
  • Inserting a record into students table
     INSERT INTO students VALUES (1, 'Geeks'); 
  • Reading records from the students table
     SELECT * FROM students; 


  • Updating records in students table
     UPDATE students SET name="GeeksforGeeks" WHERE id = 1; 

  • Deleting records from students table
     DELETE FROM students WHERE id = 1; 

2. MongoDB (Cross-platform Document-Oriented Database) :
MongoDB is a NoSQL database where each record is a document comprising of key-value pairs that are similar to JSON objects with schemas. MongoDB is flexible and allows its users to create schema, databases, tables, etc. Documents that are identifiable by a primary key make up the basic unit of MongoDB. Once MongoDB is installed, users can make use of Mongo shell as well. Mongo shell provides a JavaScript interface through which the users can interact and carry out CRUD operations.

In other words we can say that MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era licensed under the Server Side Public License.



Why use MongoDB?

  • Scalable and can serve several machines.
  • It is JavaScript Based which makes it easier to use.
  • Has Faster response because it is a document-oriented database.
  • Simpler Environment Setup
  • It uses JSON syntax which is very easy to use and has a wide range of browser compatibility.
  • Data is stored in the form of JSON whether it is Objects, Object Members, Arrays, Values and Strings.
  • Uber and Stack Comapnies uses MongoDB!


Writing queries in MongoDB:

  • Creating a student database
     use student; 
  • Creating a students table
     db.createCollection("students"); 
  • Inserting records into the students collection
     db.students.insert 
    (
        {
            "id" : 1,
            "Name" : "Harry",
                     "Team": "Geeks For Geeks"
        }
    ); 
  • Reading from the students collection
     db.students.find({Name : "Harry"}).forEach(printjson); 



Difference between PostgreSQL and MongoDB :

MongoDB PostgreSQL
MongoDB was written in C++ PostgreSQL was written in C
MongoDB was started in 2007 by 10gen, which created the product based on the word humongous PostgreSQL is an open-source project maintained by PostgreSQL Global Development Group and their prolific community
MongoDB offers the Community Support Forum, ServerFault, and StackOverflow. Users can also get enterprise support 24×7 via Enterprise grade support. PostgreSQL has a wide variety of community and commercial support options available for users. The Community support includes mailing lists and IRC
Non-Relational Database Management System Relational Database Management System
Document Oriented Object Oriented
Only Available in English Language Available in Multi Languages

Which is best and why?

PostgreSQL is best when : You need Standard compliant, transactional and ACID (Atomicity, Consistency, Isolation and Durability) compliant out of the box which also has a wide support for NoSQL features.

MongoDB is best when : You need Scalability and caching for real-time analytics but is not built for transactional data i.e., accounting systems.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.