Sub topic 2
Installing PostgreSQL on Your System
This chapter assumes that you can open a terminal or command prompt, run commands, and install software with administrator permission. You should also know that a database stores organized data and that a table stores related rows and columns.
Choose an installation method
PostgreSQL has two main parts:
- The server stores data and executes SQL statements.
- The client is a program that connects to the server.
psqlis PostgreSQL's command-line client.
Use the installation method for your operating system.
| System | Installation method | Typical command or action |
|---|---|---|
| Ubuntu or Debian Linux | APT package manager | `sudo apt install postgresql postgresql-contrib` |
| macOS | Homebrew | `brew install postgresql` |
| Windows | Official PostgreSQL installer | Download the installer from `postgresql.org` |
The official Windows installer asks you to choose a password for the initial administrative database role. Save that password. You will need it when connecting with psql.
Install PostgreSQL
Ubuntu or Debian Linux
Update the package list, then install PostgreSQL:
sudo apt update
sudo apt install postgresql postgresql-contribThe package normally creates a PostgreSQL service automatically. Do not assume that the service is running yet. You will check that in the next chapter.
macOS with Homebrew
If Homebrew is already installed, run:
brew update
brew install postgresql
brew services start postgresqlThe brew services start command starts PostgreSQL now and configures it to start when you sign in. Homebrew may install a versioned package such as postgresql@17 instead. Follow the version shown by Homebrew and use its displayed service command.
Windows
Download the Windows installer from the PostgreSQL website and run it as an administrator. Keep the default selections for the server and command-line tools. During setup:
- Choose a data directory.
- Set a password for the initial administrative role.
- Keep port
5432unless another program already uses it. - Allow the installer to add PostgreSQL tools to your
PATHwhen offered.
PATH is the list of directories that your command prompt searches for programs. If psql is not found after installation, open a new PowerShell window. The installer may also provide SQL Shell, which opens psql for you.
Verify the installed tools
Open a new terminal or command prompt and print the client version:
psql --versionA successful result resembles this:
psql (PostgreSQL) 17.2The exact version can differ. The command verifies that the psql client is installed and available on your PATH. It does not prove that the database server is accepting connections.
Common installation problems
psql: command not found: Install the client package or add PostgreSQL'sbindirectory toPATH, then open a new terminal.- Permission denied: Repeat the installation command with administrator privileges, such as
sudoon Linux. - Package not found: Refresh the package manager's indexes and check that your operating system version is supported.
- Installer asks for a password: This is the password for a database role, not necessarily your computer login password.
Exercise
What does a successful psql --version result prove, and what does it not prove?
Show answer
It proves that the psql client is installed and discoverable through PATH. It does not prove that the PostgreSQL server is running or accepting connections.