Skip to content
EL
Contents

Install PostgreSQL and learn the environment

What you are connected to

ConceptFoundation2 min read5 min hands-on#fundamentals#sql#operations

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

LayerWhat it isIn our setup
ServerThe running PostgreSQL programStarted by brew services
ClusterEverything one server manages, kept in one data directory/opt/homebrew/var/postgresql@18
DatabaseA separate collection of schemas and tablespostgres, shop
SchemaA namespace inside a databasepublic
TableWhere rows livecustomers, orders, …

Two rules follow from this structure:

  • A connection is to one database. Connected to postgres, you cannot see shop’s tables at all; you must connect to shop. “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:

SettingMeaningDefault
hostWhich machine the server is onYour own machine
portWhich network port it listens on5432
userWhich role to connect asYour operating-system username
dbnameWhich databaseSame 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

  1. You created customers while connected to shop. Connected to postgres, SELECT * FROM customers says the relation does not exist. Why?

    Show answer

    A connection only sees the one database it connected to. customers is in the shop database; you need to connect to shop.

  2. What is the public schema?

    Show answer

    The schema every new database contains, and where PostgreSQL puts tables when you do not name a schema.

  3. What does postgresql://localhost/shop connect to?

    Show answer

    The shop database 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. 1.
    PostgreSQL documentation: 5.10 Schemas
    Primary· §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. 2.
    PostgreSQL documentation: 2.2 Concepts
    Primary· §2.2 Concepts

    Databases managed by one server instance form a database cluster.

  3. 3.
    PostgreSQL documentation: 19.3 Connections and Authentication
    Primary· port

    The server listens on TCP port 5432 by default.

  4. 4.
    PostgreSQL documentation: 32.1 Database Connection Control Functions
    Primary· §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. 1.
    PostgreSQL documentation: 5.10 Schemas
    Primary· §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.