How is PostgreSQL organised: cluster, database, schema and table?
BeginnerConceptPostgreSQL
Answer
Short answer
A cluster is one running PostgreSQL server (one data directory, one port). It contains many databases; each connection goes to one database. Inside a database are schemas — namespaces/folders, public by default — and inside schemas are tables, views and other objects. Roles (users) are shared across the whole cluster.
Picture
cluster (port 5432)
├── roles: postgres, shop_app, reporting ← cluster-wide
├── database: shop
│ ├── schema: public → tables: customers, orders
│ └── schema: audit → tables: events
└── database: sonarqube
└── schema: public → ...
Commands
\l -- databases
\c shop -- connect to a database
\dn -- schemas in this database
\dt public.* -- tables in schema public
SELECT * FROM audit.events LIMIT 5; -- schema-qualified name
SHOW search_path; -- which schemas are searched by default
Difference from MySQL
In MySQL, "schema" = "database". In Postgres, schemas are folders inside a database, and you can't query across databases in one query (use schemas instead).
What to say in the interview
"One cluster holds many databases; each database has schemas, with public as default, and schemas hold tables. Roles are cluster-wide. Unlike MySQL, schema and database are different levels, and cross-database queries need extensions like postgres_fdw."
More on PostgreSQL
How do you set up PostgreSQL and log in with psql?BeginnerHow do you create a database, tables, users and query data in PostgreSQL?BeginnerHow do you back up and restore a PostgreSQL database?BeginnerWhat is PostgreSQL and why is it so popular?BeginnerHow do you connect to PostgreSQL from the command line?BeginnerWhat are the most useful psql meta-commands?Beginner