Back to blog

What the PostgreSQL Server Is Actually Doing

Jul 11, 2026
7 min read

What the PostgreSQL Server Is Actually Doing

When you run CREATE DATABASE, PostgreSQL creates a directory. When you run INSERT, it writes a row to a file and logs the write for crash safety. When you run SELECT, it reads that file back. That's the whole trick, once you see it, the "magic" disappears.


What the PostgreSQL Server Is Actually Doing

Most developers use PostgreSQL for years without ever forming a picture of what it's doing when a query runs. SQL goes in, rows come out, and everything in between feels like a sealed box. It isn't a sealed box. It's a program, running on a computer, reading and writing files, same as any other program you've written. This article builds that basic picture, in plain language, with no prior database knowledge assumed.

A Database Is a Directory

Run CREATE DATABASE learning_postgres and PostgreSQL doesn't do anything exotic. It creates a directory on disk to hold that database's data. That's it. Every database you create gets its own folder, sitting under PostgreSQL's data directory, the same way any application on your computer might create a folder to store its files.

This is worth sitting with, because it quietly answers a question that trips up a lot of people: "where does my data actually live?" It lives in a directory on the disk of whatever machine is running the PostgreSQL server. Not in the cloud in some abstract sense, not floating in "the database." On disk, in files, in a folder PostgreSQL controls.

It also explains something you may have noticed without thinking about it: CREATE DATABASE returns almost instantly, milliseconds, not minutes. That's because creating a database is just creating an empty directory. There's no data in it yet. The real work, and the real cost, only begins once rows start filling those files.

A Table Is a File, a Row Is Bytes in That File

Inside that directory, each table you create is backed by its own file (large tables get split across several, but the idea holds). When you run:

INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com');

PostgreSQL takes that row and writes it, as bytes, into the file backing the users table. Nothing more mysterious than that.

When you run:

SELECT * FROM users WHERE email = 'alice@example.com';

PostgreSQL opens that file (or consults an index that points into it, more on indexes in a later module), scans through the rows stored there, checks each one against your WHERE condition, and returns the ones that match.

Think of the file as a very disciplined spreadsheet that only PostgreSQL is allowed to touch directly. You never open it yourself, you always go through SQL, but structurally, that's what it is: rows, stored as bytes, in a file, on disk.

Under the hood, PostgreSQL is really answering a short chain of questions before it hands you an answer: Is the data I need already sitting in memory, or do I have to go to disk for it? Is there a shortcut, an index, that gets me there without reading the whole file? And once I've found a row, is it actually the current, correct version, or an old one nobody needs anymore? You don't write any of that logic. But knowing it happens is what lets you start reasoning about why one query is fast and an almost-identical one is slow.

PostgreSQL Is a Process, Not a Black Box

When you connect to PostgreSQL, whether from psql, a Node.js app, or a GUI tool, you're talking to a running process on a server. That process listens for your connection, hands you off to a dedicated worker just for your session, and from that point on, every query you send is handled by an ordinary program doing ordinary things: reading bytes from files, writing bytes to files, holding some of that data in memory so it doesn't have to touch the disk every single time.

That last part, keeping frequently-used data in memory, is why the second time you query something is usually much faster than the first. The process remembers what it recently read from disk and serves it from memory instead. This is the same idea as any caching you've done in application code, just built into the database itself.

This is also why a query can get slower for no reason you can find in your SQL. If PostgreSQL just restarted, deployed, crashed, rebooted, that memory is empty again. Nothing is cached yet. The first round of queries has to go back to disk to rebuild that cache from scratch, before things speed back up. If you've ever seen a database feel sluggish right after a restart and assumed you'd broken something, this is usually the real reason: the cache went cold, not your code.

The Log That Saves You From Crashes

Here's the one detail that separates "just writing to a file" from what a production database actually needs to guarantee: what happens if the power goes out, or the process crashes, in the middle of a write?

PostgreSQL's answer: before it changes the actual data file, it first writes down what it's about to do, to a separate log. Only after that note is safely on disk does it go ahead and make the real change.

If the server crashes mid-write, it doesn't matter, on restart, PostgreSQL reads that log and replays anything that didn't finish. It's the same instinct as keeping a to-do list before starting a task: if you get interrupted, you don't have to remember what you were doing, you just check the list. This log is one of the reasons people trust PostgreSQL with data they can't afford to lose.

Why This Mental Model Matters

Once "PostgreSQL is a process that reads and writes files, with a safety log protecting every write" is in your head, a lot of things that used to feel like separate pieces of magic start looking like variations on the same idea:

  • Indexes are just extra files that let PostgreSQL find the right rows without scanning the whole table.
  • Caching (shared buffers) is just PostgreSQL keeping recently-used file contents in memory.
  • Replication is just another server reading that same safety log and replaying it, to keep a second copy of the data in sync.
  • VACUUM is just PostgreSQL cleaning up old row versions it no longer needs, so the files don't grow forever.

None of these are separate systems bolted onto a mysterious core. They're all extensions of the same basic loop: read files, write files, log before you write, keep useful things in memory.

PostgreSQL isn't magic. It's a server process that manages files intelligently, and every advanced feature it has is that idea, applied more cleverly.

Where This Fits in the Full Course

This is the very first mental model from the Foundation phase of the PostgreSQL In-Depth course, the phase built for zero prior database knowledge. If this clicked, the next modules build on it directly: the client-server connection model in detail, the relational mental model (tables, rows, and how they connect), and your first complete schema.

For readers who want the advanced version of this same territory, process architecture, MVCC, the planner, replication internals, see "The PostgreSQL Elephant in the Room." That article assumes you already have the basic picture this one just gave you.

The Question Worth Asking Yourself

Next time a PostgreSQL query does something you don't expect, slow, fast, returning stale-looking data, try asking the simplest possible question first: what files is it reading or writing right now, and what would that look like from the outside?

You'll be surprised how often that question, on its own, points you toward the answer, long before you need to reach for anything more advanced.

#PostgreSQL #Database #Backend #SoftwareEngineering #DatabaseFundamentals #LearnToCode #SQL

Discussion

0

Join the discussion

Loading comments...