Table of Contents
Open Table of Contents
Introduction
I’ve been learning PostgreSQL with the book Just Use Postgres! by Denis Magda, and one of the first things that clicked for me was how a PostgreSQL installation is organized. Words like server, cluster, database and schema show up everywhere, and they’re easy to mix up. This post is my attempt to put them in order.
PostgreSQL is an open-source RDBMS, a Relational Database Management System. Let’s unpack that:
- Relational: data is stored in relations, which we usually call tables. A table has columns (with a name and a data type) and rows (the actual records).
- Database Management System: the software that stores that data on disk and lets you query and change it, usually with SQL. It also takes care of things you don’t want to build yourself, like handling many concurrent users, enforcing constraints and recovering after a crash.
So PostgreSQL is the software. What it manages is organized in layers, and that’s what the rest of this post is about.
Understanding PostgreSQL’s hierarchy
From the outside in, a PostgreSQL installation looks like this:
The cluster
When PostgreSQL is initialized (with the initdb command, or by your package manager or Docker image on your behalf), it creates a database cluster: a data directory on disk that holds a collection of databases, managed by a single running PostgreSQL server.
The name can be misleading. In PostgreSQL’s vocabulary, a cluster is not a group of machines and it’s not a set of application servers. It’s simply “all the databases served by one PostgreSQL server”. If you start a second server with a different data directory and port, you have a second, independent cluster, even on the same machine.
Databases
A cluster contains one or more databases. Each database is a separate, isolated container for data. When you connect to PostgreSQL, you always connect to one specific database, and a regular query can only see objects in that database.
Right after initialization, you’ll find a database named postgres. It’s an ordinary, empty database created so that users and tools always have somewhere to connect to by default. You’ll also see template0 and template1, which PostgreSQL uses as templates when you run CREATE DATABASE. You don’t need to worry about them for now.
In the diagram, bakery and coffee_shop are two databases we created ourselves. They live in the same cluster, but they don’t share tables.
Schemas
Inside each database there are one or more schemas. A schema is a named container for database objects: tables, views, indexes, sequences, functions and so on.
Every new database comes with a schema called public. In a default setup, when you create a table without saying which schema it belongs to, it ends up in public. That’s why many projects never think about schemas at all: everything quietly lives in public.
Tables (and other objects)
Finally, tables live inside schemas. So every table has a full address:
cluster → database → schema → table
Within a database, you refer to a table as schema_name.table_name, for example products.breads.
What is a schema?
Conceptually, a schema is a namespace: a way to group related objects under a common name and avoid name collisions.
If you come from programming, a useful analogy is a package in Go or Java, or a module in Python. Just as you can have orders.Item and inventory.Item in two different packages, in PostgreSQL you can have sales.items and warehouse.items in the same database without any conflict. The schema name tells PostgreSQL (and the people reading your SQL) which one you mean.
It’s only an analogy, though. Schemas don’t have imports, they can’t be nested inside other schemas, and they don’t encapsulate behavior the way modules do. They’re a flat, one-level way of organizing objects within a database.
The important difference from databases is this: schemas in the same database can be queried together. You can join products.breads with catalog.categories in a single query. Tables in two different databases can’t be joined that way, because your connection only sees one database.
A practical example
Let’s build the bakery database from the diagram. You can run these commands in psql, PostgreSQL’s command-line client.
First, create the database and connect to it:
CREATE DATABASE bakery;
-- psql meta-command: connect to the new database
\c bakery
Now create two schemas of our own:
CREATE SCHEMA products;
CREATE SCHEMA catalog;
Then create a table in each schema, using the schema.table notation:
CREATE TABLE catalog.categories (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE products.breads (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
price numeric(6, 2) NOT NULL,
category_id integer REFERENCES catalog.categories (id)
);
Notice that products.breads references a table in a different schema. That’s perfectly fine, because both schemas live in the same database.
Let’s add some data and query across both schemas:
INSERT INTO catalog.categories (name) VALUES ('Sourdough');
INSERT INTO products.breads (name, price, category_id) VALUES ('Country loaf', 6.50, 1);
SELECT b.name, b.price, c.name AS category
FROM products.breads AS b
JOIN catalog.categories AS c ON c.id = b.category_id;
Two handy psql commands to look around:
\l -- list the databases in the cluster
\dn -- list the schemas in the current database
If you run \dn now, you’ll see catalog, products and the public schema that came with the database.
A brief introduction to multi-tenancy
Schemas become especially interesting when you build software used by several customers, or tenants, and want to keep each tenant’s data apart. One common approach is schema per tenant: every tenant gets its own schema with the same set of tables.
CREATE SCHEMA tenant_acme;
CREATE SCHEMA tenant_globex;
CREATE TABLE tenant_acme.orders (id integer PRIMARY KEY, total numeric(10, 2));
CREATE TABLE tenant_globex.orders (id integer PRIMARY KEY, total numeric(10, 2));
Both tables are called orders, but they live in different namespaces, so they never collide. The application decides which schema to use for each request. This gives you a clear separation between tenants while still running everything in a single database.
It’s one strategy among several, each with trade-offs:
- Shared tables: all tenants share the same tables, and each row carries a
tenant_idcolumn. It’s the simplest to operate, but every query must filter by tenant. - Schema per tenant: the approach above. Good separation, but migrations have to be applied to every schema, which gets harder as the number of tenants grows.
- Database per tenant: each tenant gets a whole database in the cluster (like
bakeryandcoffee_shopin the diagram). It gives the strongest isolation, but it’s heavier to manage, and you can’t easily query across tenants.
Which one fits depends on how many tenants you have, how isolated they need to be, and how much operational work you’re willing to take on.
Conclusion
Here’s the mental model to take away:
- PostgreSQL is the RDBMS, the software.
- A cluster is the set of databases managed by one running PostgreSQL server.
- A database is an isolated container. You connect to one at a time.
- A schema is a namespace inside a database.
publicexists by default, and you can create your own. - Tables (and views, indexes, functions…) live inside schemas.
Once this hierarchy is clear, a lot of PostgreSQL’s documentation and tooling starts to make much more sense.