Sub topic 1
How a PostgreSQL server is organized
PostgreSQL uses a layered server model. A PostgreSQL server is a running database service. It can contain several databases. Each database contains schemas, and schemas contain objects such as tables, views, sequences, indexes, functions, and types.
Prerequisites
You should know basic SQL, including SELECT, WHERE, JOIN, and ORDER BY. You should also understand tables, columns, primary keys, and foreign keys. This chapter assumes you can use a terminal and have access to a PostgreSQL server.
The object hierarchy
A client such as psql, pgAdmin, or an application connects to a PostgreSQL server through a host and port. The usual PostgreSQL port is 5432.
A server cluster is the collection of databases managed by one PostgreSQL server instance. A connection normally selects one database. SQL commands run inside that database and cannot directly query tables in another database.
A database contains one or more schemas. A schema is a namespace that groups objects and prevents name conflicts. PostgreSQL creates a public schema in a new database by default. For example, both sales.orders and archive.orders can exist because their schema-qualified names differ.
A table stores rows and columns. Other objects support or expose table data:
- A view is a saved query that behaves like a table when queried.
- An index is a data structure that helps PostgreSQL find rows efficiently.
- A sequence generates ordered values, often for key columns.
- A function stores reusable server-side logic.
- A constraint enforces rules such as uniqueness or valid foreign-key references.
Names and search paths
You can refer to an object with a qualified name:
SELECT *
FROM public.orders;The first part, public, is the schema name. The second part, orders, is the table name. Qualification is useful when inspecting an unfamiliar database because it removes ambiguity.
When you omit the schema, PostgreSQL searches the schemas in the current session's search_path:
SHOW search_path;A common result includes "$user" and public. This means an unqualified reference such as orders may resolve to public.orders, but only if no earlier schema contains a matching table.
Ownership and access
Every database object has an owner. Roles control authentication and authorization. A role may be allowed to connect to a database, use a schema, or read and modify a table. Ownership and privileges are separate from the object hierarchy: seeing an object in a tool does not guarantee that you can read its data.
A useful inspection model
When entering an unfamiliar environment, move from broad to narrow:
- Identify the server and the database connection.
- List the schemas in that database.
- Find tables and views in the relevant schema.
- Inspect columns, constraints, indexes, and ownership.
- Check privileges before assuming that an object is usable.
This model works in both psql and pgAdmin. The next chapters apply it with command-line meta-commands and graphical navigation.
Exercise
In the name accounting.invoices, which part is the schema and which part is the object name?
Show answer
accounting is the schema name, and invoices is the object name. Together they identify the table or other relation as accounting.invoices.