Sub topic 2
Installing PostgreSQL on your system
This chapter assumes you can run commands in a terminal and have administrator rights on your machine (sudo on Linux and macOS, an administrator account on Windows). You don't need to know SQL yet. By the end you will have a running PostgreSQL server, a working psql client, and a check that both are installed correctly.
What you are installing
A PostgreSQL installation has two halves that package managers sometimes split apart:
- The server is a background process (
postgres) that stores data and answers queries. It listens on TCP port 5432 by default. - The client tools are programs you run to talk to the server. They include
psql(the interactive shell),pg_isreadyandpg_dump.
On a single development machine you want both. Later chapters connect to localhost, so the server and client will run on the same computer.
The installer also creates a cluster. This is a data directory that holds one server's databases, roles and configuration. Where the cluster is created, and who owns it, depends on the platform. That difference explains most first-day confusion.
Choosing an install path
Pick the major version first. PostgreSQL releases one major version per year, and each is supported for five years. Examples here use 17. Replace it with a version from the project's supported list at postgresql.org if you want a newer one. Minor updates within a major version (17.1, 17.2 and so on) only fix bugs and are safe to apply. Moving between major versions needs a planned upgrade.
Linux
Debian and Ubuntu
sudo apt update
sudo apt install postgresqlThe postgresql package installs the latest version your distribution ships. The Debian packaging then does three things for you:
- It creates a cluster and starts it.
- It enables the service at boot.
- It creates an operating-system user called
postgresthat owns the data.
List the clusters with the helper that Debian provides:
pg_lsclustersThe output shows the version, cluster name (main), port, status (online) and data directory. If you need a newer major version than your distribution offers, use the PostgreSQL project's own apt repository (PGDG) instead of a third-party download.
Fedora and RHEL-family systems
The RPM packages install the software but do not create a cluster. You must initialize one and start the service yourself:
sudo dnf install postgresql-server
sudo postgresql-setup --initdb
sudo systemctl enable --now postgresql--initdb creates the data directory. enable --now starts the service immediately and also at every boot. Skipping the initialization step is the usual reason the service fails to start on these systems.
macOS
Homebrew
brew install postgresql@17
brew services start postgresql@17Homebrew installs versioned PostgreSQL formulas as "keg-only". The binaries are not linked into your default PATH, so psql will report "command not found" until you add the directory. On Apple Silicon:
echo 'export PATH="/opt/homebrew/opt/postgresql@17/bin:$PATH"' >> ~/.zshrc
source ~/.zshrcOn an Intel Mac, use /usr/local/opt/postgresql@17/bin instead. If you are unsure, brew --prefix postgresql@17 prints the base path.
Postgres.app
Postgres.app is a drag-and-drop application from postgresapp.com. It runs the server as your own user, and you start and stop it from a menu-bar icon. It is the simplest option if you want no service management. You still need to add its bin directory to your PATH to use psql from a regular terminal. The app's documentation gives the exact path.
Windows
Use the graphical installer from the PostgreSQL download page, which is built by EDB. The wizard asks for four things:
- Components. Keep PostgreSQL Server and Command Line Tools. pgAdmin and Stack Builder are optional.
- Superuser password. This is the password for the
postgresrole. Write it down, because you will need it every time you connect with that role. - Port. Accept 5432 unless another PostgreSQL is already using it.
- Locale. The default is fine for practice.
The installer registers a Windows service named like postgresql-x64-17 and starts it. It places the tools in C:\Program Files\PostgreSQL\17\bin, which is not added to PATH automatically. Add it through Environment Variables, or call the tools by full path. Open a new PowerShell window afterwards so the change takes effect, then check the service:
Get-Service postgresql*A Status of Running means the server is up.
Alternative: a container
If you prefer not to install system-wide, Docker can run a disposable server:
docker run --name pg-practice -e POSTGRES_PASSWORD=change-me -p 5432:5432 -d postgres:17The -p 5432:5432 option publishes the container's port on your machine. Without it, nothing on your host can reach the server. The data disappears when you remove the container unless you mount a volume. This tutorial assumes a native install, but the later connection steps work the same against a container.
Verifying the installation
Run two commands. The first checks the client, and the second checks the server.
psql --version
pg_isreadypsql --version prints something like psql (PostgreSQL) 17.2. It proves the client is installed and on your PATH. It says nothing about the server.
pg_isready contacts the server on the default host and port without logging in. It reports a result such as /var/run/postgresql:5432 - accepting connections and sets an exit code you can use in scripts:
| Exit code | Meaning |
|---|---|
| 0 | The server is accepting connections |
| 1 | The server is running but rejecting connections, for example during startup |
| 2 | No response from the server |
| 3 | No attempt was made, usually because of invalid parameters |
If you installed a client but no server, or the server is stopped, you will see exit code 2 and a "no response" message. The next chapter covers checking and starting the service in detail.
Common installation problems
psql: command not found. The binaries are installed but not on PATH. This is typical for Homebrew and Windows. Fix PATH as shown above.- Client and server versions differ. A
psqlfrom version 16 can talk to a version 17 server, and the reverse also works. Backslash commands that query the system catalogs may show incomplete results across large version gaps. Prefer matching versions when you can. - Port 5432 already in use. Another PostgreSQL (or a container) occupies the port. A second installation will pick another port or refuse to start. Check with
pg_lsclusterson Debian, or note the port in the installer. - Two installations on one machine.
psql --versionmay report a different version from the server you meant.which psql(orwhere psqlon Windows) shows which binary runs first.
Exercise
After installing PostgreSQL from Homebrew, psql --version prints "command not found", although brew list shows postgresql@17. What is the cause and what do you do?
Show answer
Versioned Homebrew PostgreSQL formulas are keg-only, so their bin directory is not on PATH. Add the directory to your shell profile, for example export PATH="/opt/homebrew/opt/postgresql@17/bin:$PATH" on Apple Silicon (use /usr/local/opt/postgresql@17/bin on Intel), then open a new terminal or run source ~/.zshrc. Check with psql --version, then run pg_isready to confirm the server is also running.
Exercise
psql --version works, but pg_isready exits with code 2. Does the installation of the client fail? What should you check next?
Show answer
No. The client is installed correctly, but the server is not responding. Check that the server service is running (for example systemctl status postgresql, brew services list, or Get-Service postgresql*), that a cluster was initialized (required on Fedora and RHEL), and that you are checking the correct host and port.