Install PostgreSQL and learn the environment
Lab: install PostgreSQL and connect
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;
| Ours | Yours | |
|---|---|---|
| Customers | 8 | |
Products (SELECT count(*) FROM products;) | 11 | |
| Orders | 12 | |
| Order items | 19 |
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
| Symptom | Likely cause | Fix |
|---|---|---|
psql: command not found | The PATH line did not take effect | Open a new terminal, or run source ~/.zshrc |
connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory | The server is not running | brew services start postgresql@18, then brew services list |
database "shop" does not exist | createdb shop was skipped | Run it, then load the file again |
Check yourself
What does
brew services start postgresql@18do that runningpsqldoes not?Show answer
It starts the PostgreSQL server, the program that owns the data, and keeps it running in the background.
psqlis only a client that connects to a running server.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.
What does
psql -d shop -f shop.sqldo?Show answer
It connects to the
shopdatabase and runs every SQL statement in the fileshop.sql.
Finished reading?
Saved in this browser only.
Sources
Where each claim on this page comes from.
- 1.Homebrew formula: postgresql@18Primary· Caveats·
brew info postgresql@18The formula is keg-only, creates a default database cluster under the Homebrew prefix, and runs as a service with brew services.
- 2.PostgreSQL documentation: initdbPrimary· Options: --username
The first superuser is named after the operating-system user that created the cluster.
- 3.PostgreSQL documentation: 18.2 Creating a Database ClusterPrimary· §18.2 Creating a Database Cluster
A new cluster contains a database named postgres, meant as a default database for users and tools.
- 4.
- 5.PostgreSQL documentation: psqlPrimary· Options: -d, -f
psql connects to the named database and can run the commands in a file.
- 6.PostgreSQL downloadsPrimary
Official installation instructions and packages for every operating system.
Go further
What to read next, and why it is worth the time.
- 1.
Applies to PostgreSQL 18. Draft: not yet fact-checked end to end.