Skip to content
EL
Contents

Install PostgreSQL and learn the environment

Lab: install PostgreSQL and connect

LabFoundation3 min read20 min hands-on#fundamentals#sql#operations

The idea in 30 seconds

Install PostgreSQL 18, start it as a background service, connect with psql, and load the practice shop database. Twenty minutes from nothing to your first query.

Helps to know first: Relational database concepts

On this page

Install

macOS with Homebrew

This is the path we tested, on macOS with Apple Silicon and PostgreSQL 18.3.

brew install postgresql@18

Homebrew installs postgresql@18 as keg-only: the programs are not put on your PATH, so a shell cannot find psql yet. Add them:

echo "export PATH=\"$(brew --prefix postgresql@18)/bin:\$PATH\"" >> ~/.zshrc
source ~/.zshrc
psql --version

The install also created a database cluster: a data directory, already initialized, where PostgreSQL keeps everything. Start the server as a background service, so it runs now and after every restart:

brew services start postgresql@18

Linux, Windows, or Docker

Use the official instructions at postgresql.org/download, which cover each Linux distribution, the Windows installer, and other systems. We have not tested those routes in this lab, so follow the official page rather than a copy of it. Pick version 18 so your results match this field.

Connect

On macOS with Homebrew, the server already has a superuser named after your macOS account, so you can connect without a password:

psql postgres

postgres is a database every new cluster contains, meant as a default for users and tools to connect to. You should see a prompt like postgres=#. Type \q to leave.

Check what you are connected to:

SELECT current_user, current_setting('port'), current_setting('data_directory');

On our machine that returned apple | 5432 | /opt/homebrew/var/postgresql@18: our macOS username, the default port, and the data directory Homebrew created.

Load the practice database

Download shop.sql into your working folder, then:

createdb shop
psql -d shop -f shop.sql
psql shop

createdb makes a new, empty database; psql -d shop -f shop.sql runs every statement in the file against it. The first time, you will see a few NOTICE: table "…" does not exist, skipping lines: the file drops its tables before creating them, and on a fresh database there is nothing to drop. That is expected.

Now run your first query:

SELECT count(*) FROM customers;
OursYours
Customers8
Products (SELECT count(*) FROM products;)11
Orders12
Order items19

If your counts match, you have the same data this whole section uses, and every result shown on later pages should match yours exactly.

If something goes wrong

SymptomLikely causeFix
psql: command not foundThe PATH line did not take effectOpen a new terminal, or run source ~/.zshrc
connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directoryThe server is not runningbrew services start postgresql@18, then brew services list
database "shop" does not existcreatedb shop was skippedRun it, then load the file again

Check yourself

  1. What does brew services start postgresql@18 do that running psql does not?

    Show answer

    It starts the PostgreSQL server, the program that owns the data, and keeps it running in the background. psql is only a client that connects to a running server.

  2. Why can you connect on macOS without creating a user or password first?

    Show answer

    The cluster was initialized by your macOS account, and its first superuser is named after the operating-system user that created it. Homebrew’s default setup lets that local user connect.

  3. What does psql -d shop -f shop.sql do?

    Show answer

    It connects to the shop database and runs every SQL statement in the file shop.sql.

Finished reading?

Saved in this browser only.

Sources

Where each claim on this page comes from.

  1. 1.
    Homebrew formula: postgresql@18
    Primary· Caveats· brew info postgresql@18

    The formula is keg-only, creates a default database cluster under the Homebrew prefix, and runs as a service with brew services.

  2. 2.
    PostgreSQL documentation: initdb
    Primary· Options: --username

    The first superuser is named after the operating-system user that created the cluster.

  3. 3.
    PostgreSQL documentation: 18.2 Creating a Database Cluster
    Primary· §18.2 Creating a Database Cluster

    A new cluster contains a database named postgres, meant as a default database for users and tools.

  4. 4.
    PostgreSQL documentation: createdb
    Primary

    createdb creates a new database.

  5. 5.
    PostgreSQL documentation: psql
    Primary· Options: -d, -f

    psql connects to the named database and can run the commands in a file.

  6. 6.
    PostgreSQL downloads
    Primary

    Official installation instructions and packages for every operating system.

Go further

What to read next, and why it is worth the time.

  1. 1.
    PostgreSQL downloads
    Primary

    The official, current instructions for Linux, Windows and other systems.

Applies to PostgreSQL 18. Draft: not yet fact-checked end to end.