Install PostgreSQL and learn the environment
What you are connected to
The idea in 30 seconds
One PostgreSQL server runs one cluster. A cluster holds databases, a database holds schemas, and a schema holds tables. Roles belong to the whole cluster, but every connection is to exactly one database. Knowing which layer you are in explains most "table does not exist" surprises.
Helps to know first: Relational database concepts
On this page
The layers
| Layer | What it is | In our setup |
|---|---|---|
| Server | The running PostgreSQL program | Started by brew services |
| Cluster | Everything one server manages, kept in one data directory | /opt/homebrew/var/postgresql@18 |
| Database | A separate collection of schemas and tables | postgres, shop |
| Schema | A namespace inside a database | public |
| Table | Where rows live | customers, orders, … |
Two rules follow from this structure:
- A connection is to one database. Connected to
postgres, you cannot seeshop’s tables at all; you must connect toshop. “Relation does not exist” often just means you are connected to the wrong database. - Roles belong to the whole cluster. A user created once can be allowed into some databases and not others.
Inside a database, tables live in schemas. If you do not name one, PostgreSQL puts new tables in a schema called public, which every new database has. That is why \dt lists public | customers. For now, public is all you need.
Describing a connection
To connect, a client needs four things:
| Setting | Meaning | Default |
|---|---|---|
| host | Which machine the server is on | Your own machine |
| port | Which network port it listens on | 5432 |
| user | Which role to connect as | Your operating-system username |
| dbname | Which database | Same as the user name |
Application code usually packs these into one connection URI:
postgresql://app_user@db.example.com:5432/shop
That reads: connect as app_user to the server at db.example.com, port 5432, database shop. Every part is optional, and anything you leave out falls back to a default: postgresql://localhost/shop connects to shop on your own machine, as your own user, on port 5432.
A URI can also carry a password, but keep it out. Connection strings end up in logs, error messages, screenshots and chat. Give the password to the client separately, for example in a password file or a secret your platform injects at runtime.
Check yourself
You created
customerswhile connected toshop. Connected topostgres,SELECT * FROM customerssays the relation does not exist. Why?Show answer
A connection only sees the one database it connected to.
customersis in theshopdatabase; you need to connect toshop.What is the
publicschema?Show answer
The schema every new database contains, and where PostgreSQL puts tables when you do not name a schema.
What does
postgresql://localhost/shopconnect to?Show answer
The
shopdatabase on the server on your own machine, on the default port 5432, as your operating-system user.
Finished reading?
Saved in this browser only.
Sources
Where each claim on this page comes from.
- 1.PostgreSQL documentation: 5.10 SchemasPrimary· §5.10 Schemas
A cluster contains databases; roles are shared across the cluster; a connection can only access the one database it asked for; a database contains schemas, which contain tables; new tables go into the public schema by default.
- 2.PostgreSQL documentation: 2.2 ConceptsPrimary· §2.2 Concepts
Databases managed by one server instance form a database cluster.
- 3.PostgreSQL documentation: 19.3 Connections and AuthenticationPrimary· port
The server listens on TCP port 5432 by default.
- 4.PostgreSQL documentation: 32.1 Database Connection Control FunctionsPrimary· §32.1.1.2 Connection URIs
The general form of a postgresql:// connection URI; every part of it is optional.
Go further
What to read next, and why it is worth the time.
- 1.PostgreSQL documentation: 5.10 SchemasPrimary· §5.10
How schemas and the search path decide which table a name refers to. You will need it when a project grows beyond the public schema.
Applies to PostgreSQL 18. Draft: not yet fact-checked end to end.