← Back to Articles Technology & IT Infrastructure

PostgreSQL, RDBMS, and MongoDB: How They Differ

How RDBMS and PostgreSQL relate, how MongoDB's document model differs, and what to weigh when choosing a database based on data shape and transaction needs.

These three terms are often listed side by side, although they sit at different levels. An RDBMS is a type of database system, PostgreSQL is one product of that type, and MongoDB is a product of another type that stores data as documents. Comparing RDBMS with PostgreSQL is a little like comparing cars with one make of car.

The relational model

A Relational Database Management System (RDBMS) stores data in tables made of rows and columns. Each column has a data type, and tables can be linked through keys. PostgreSQL, MySQL, MariaDB, SQL Server, and Oracle Database all belong to this group.

Take a building supply shop. Customers and invoices live in separate tables:

CREATE TABLE customers (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name text NOT NULL
);

CREATE TABLE invoices (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  customer_id bigint NOT NULL REFERENCES customers(id),
  invoice_date date NOT NULL,
  total numeric(14,2) NOT NULL CHECK (total >= 0)
);

The customer_id column refers to the customers table. The database rejects an invoice for a customer who does not exist, and a negative total as well. Because a customer’s name is stored once, changing it takes one update. Separating data so that it is not repeated is known as normalisation.

SQL brings the tables back together, using one query language for both daily operations and reports.

Why ACID transactions matter

A single sale usually changes several things at once: an invoice is added, stock falls, and a receivable is recorded. If the power fails after the second step, the data no longer matches. A transaction prevents this by treating all the steps as one unit that either succeeds or is rolled back.

The acronym ACID summarises the guarantees: atomicity (all or nothing), consistency (rules are respected), isolation (concurrent transactions do not interfere, according to the isolation level), and durability (committed results survive a failure). Isolation levels and storage settings can be changed, so the strength of the guarantee depends on configuration.

Where PostgreSQL fits

PostgreSQL is an open-source RDBMS. It uses Multiversion Concurrency Control (MVCC), which keeps several versions of a row so that reading does not block writing. Extensions add features, such as PostGIS for location data.

PostgreSQL also stores JSON. The jsonb type keeps documents in a binary form that can be indexed, and the documentation recommends it over json for most applications. Product attributes that vary, such as clothing size or battery capacity, can go in a jsonb column while price and stock stay in ordinary columns.

There are limits. Any update locks the whole row, so a very large document that changes often can become a bottleneck. Storing everything as JSON also gives up the benefits of column types and foreign keys.

The document model in MongoDB

MongoDB stores data as BSON documents, a binary format that resembles JSON. Documents are grouped into collections, and by default the documents in one collection do not need the same fields. Data that is usually read together can be embedded in a single document:

{
  "number": "INV-0012",
  "customer": { "name": "Toko Sinar", "city": "Makassar" },
  "items": [
    { "sku": "PK-01", "qty": 3, "price": 24000 },
    { "sku": "CT-07", "qty": 1, "price": 85000 }
  ]
}

Reading that invoice takes one document and no joins. In exchange, the customer name is copied into many invoices, so the application must decide whether older copies should be updated or kept as a record of the moment of sale.

Some older assumptions about MongoDB are out of date. It supports multi-document transactions on replica sets and sharded clusters, although the documentation warns that they generally cost more than single-document writes and do not replace good schema design. It also offers schema validation to reject data that breaks the rules. A single document is limited to 16 MiB.

For very large data sets, MongoDB has built-in sharding that spreads data across servers. The choice of shard key determines how well it works, and running a cluster adds administrative effort. Licensing deserves a look too: versions released after 16 October 2018 use the Server Side Public License (SSPL), while PostgreSQL has its own permissive licence.

Choosing between them

SituationUsually the better fit
Finance, stock, and orders that depend on each otherAn RDBMS such as PostgreSQL
Cross-cutting reports that keep changingAn RDBMS
Mostly relational data with some varying attributesPostgreSQL with jsonb
Self-contained documents read as a whole, such as content or a varied catalogueMongoDB
Heavy write volume spread over many serversMongoDB or another distributed system, after testing

The table is a starting point. MongoDB is not automatically faster, and PostgreSQL is not automatically slower, because performance comes from indexes, query patterns, and data design. If the decision carries weight, test both with a copy of your own data.

Team skills and operating costs count as well. Running two kinds of database means two sets of backup, monitoring, and upgrade procedures. If the number of connections becomes a problem, see how PgPool and PgBouncer work. The link between databases and frameworks is discussed in Flask, Laravel, and Django compared.

References

← All Articles
Contact Us