PostgreSQLBeginner 12 min Lesson 2 of 40

Installing PostgreSQL

Install PostgreSQL on Windows, Ubuntu and RHEL/CentOS, then start, stop, restart and check the service, and understand versions.

PostgreSQL · Lesson 2 of 40
0/40 done(0%)

What is it? #

Installing PostgreSQL means putting two separate things on a machine: the server, which stores data and answers queries, and the client tools, which let you talk to a server.

You do not always need both. On your laptop you might install only the client, so you can connect to a server running elsewhere. On a production machine you install the server.

After installation, the server runs as a service — a background program that the operating system starts automatically, including after a reboot. Most of your day-to-day operating work is checking whether that service is running and, occasionally, restarting it.

PostgreSQL releases a new major version roughly once a year, and each major version is supported for five years.

Think of it like this #

Installing the server is like putting a working kitchen into a building. Installing the client tools is like getting a phone that can call the kitchen and place an order.

You can have the phone without the kitchen — that is a laptop talking to a database on a server. You would not normally put a kitchen in every flat when one shared kitchen already serves the building.

Simple example #

You have a fresh Ubuntu server for a small application.

You install the PostgreSQL server package, confirm the service started, check the version, and connect once with psql to prove it works. That is the whole job — about four commands, and then the database is ready for the next lesson.

Code #

BASH
# ---------- Ubuntu / Debian ----------

sudo apt update                  # refresh the list of available packages
sudo apt install postgresql postgresql-contrib
#   postgresql          -> the server plus the standard client tools
#   postgresql-contrib  -> extra official modules many guides assume you have

# The service is started automatically by the package. Confirm it:
sudo systemctl status postgresql    # look for "active (running)" or "active (exited)"
BASH
# ---------- RHEL / CentOS / Rocky / Alma ----------

sudo dnf install -y postgresql-server postgresql-contrib

# On this family the data directory is NOT created for you. Do it once:
sudo postgresql-setup --initdb      # creates the data directory and base files

sudo systemctl enable --now postgresql
#   enable -> start automatically on every boot
#   --now  -> also start it right now, so you do not have to start it separately
TEXT
---------- Windows ----------

1. Download the installer from the official PostgreSQL download page.
2. Run it. When asked, set and WRITE DOWN the password for the "postgres" user.
3. Keep the default port 5432 unless something else already uses it.
4. The installer registers PostgreSQL as a Windows service, so it starts on boot.
5. It also installs pgAdmin (a graphical client) and psql (the command-line client).

To use psql from PowerShell, add the bin folder to your PATH, for example:
  C:\Program Files\PostgreSQL\16\bin
BASH
# ---------- Managing the service (Linux) ----------

sudo systemctl status postgresql     # is it running? shows recent log lines too
sudo systemctl start postgresql      # start it
sudo systemctl stop postgresql       # stop it  <-- all databases become unreachable
sudo systemctl restart postgresql    # stop then start  <-- drops open connections
sudo systemctl reload postgresql     # re-read config WITHOUT dropping connections
sudo systemctl enable postgresql     # start automatically on boot
sudo systemctl disable postgresql    # do not start on boot

# Prefer "reload" over "restart" whenever the change allows it.
BASH
# ---------- Checking what you have ----------

psql --version        # version of the CLIENT tool
postgres --version    # version of the SERVER binary

# The version the server is actually running, asked over a connection:
sudo -u postgres psql -c "SELECT version();"

# Where is it listening?
sudo ss -tulpn | grep 5432
BASH
# ---------- First connection ----------

# On Linux the install creates an OS user called "postgres".
# Switching to that user is the normal way in:
sudo -u postgres psql

# You should land at a prompt like:  postgres=#
# Type  \q  and press Enter to leave.

How it works #

apt install postgresql fetches the packaged server and its client tools and sets up a data directory — the folder where your actual data will live. On Debian and Ubuntu this happens automatically; on the RHEL family you run postgresql-setup --initdb yourself, which is why that extra step exists.

systemctl is how modern Linux manages background services. status tells you whether PostgreSQL is running and prints the last few log lines, which is usually enough to see why it is not.

The difference between restart and reload matters more than it looks. A restart stops the server and starts it again: every open connection is dropped, and any application holding one gets an error. A reload asks the running server to re-read its configuration files without disconnecting anyone. Many settings can be changed with a reload; only some need a full restart. When you have the choice, reload.

enable and start are independent. enable only affects what happens at boot. A service can be running now but not enabled — meaning it will silently fail to come back after the next reboot. That is a genuinely common production surprise, which is why enable --now does both.

On Linux, installation creates an operating-system user named postgres, and PostgreSQL trusts local connections from that user by default. sudo -u postgres psql means "run psql as the postgres user", which is why it lets you in without typing a database password.

Client and server versions do not have to match exactly. A newer client can talk to an older server and usually the other way round. But the backup tools are the exception that bites people: to back up a version 16 server you should use version 16 (or newer) of pg_dump. Using an older pg_dump against a newer server is refused.

Real-world use #

Package managers install whichever version the operating system ships, which is often a year or two behind. That is fine for learning. For production, many teams add PostgreSQL's official repository so they can choose the major version and keep getting updates after the OS stops shipping them.

Major versions matter because the on-disk data format changes between them. You cannot simply point PostgreSQL 17 at a PostgreSQL 16 data directory — upgrading a major version is a deliberate operation involving either pg_upgrade or a dump and restore. Minor version updates (16.3 to 16.4) are just bug and security fixes and are safe to apply with a restart.

Check the version before following any tutorial, including this one. Syntax and defaults do change between major versions, and a command that fails "for no reason" is very often a version difference.

On a real server, also decide early where the data directory lives. The default is on the root filesystem, which is often small. Many production setups put the data directory on a separate, larger, monitored disk — because a database that fills its disk stops accepting writes.

Common mistakes #

  • Running systemctl start but forgetting systemctl enable, so PostgreSQL does not come back after a reboot.
  • Using restart when reload would do, needlessly dropping every open application connection.
  • Forgetting postgresql-setup --initdb on RHEL/CentOS and wondering why the service will not start.
  • Losing the postgres password set during the Windows installer and having to reset it later.
  • Backing up a newer server with an older pg_dump, which PostgreSQL refuses outright.

Practice #

On a test machine or virtual machine, install PostgreSQL. Then, without looking back at this page, do the following four things: confirm the service is running, confirm it is enabled at boot, print the server version over a connection, and connect with psql and exit again. Write down the exact commands you used for your operating system — you will reuse them constantly.

Quick quiz

  1. 1. What is the difference between `systemctl restart` and `systemctl reload`?

  2. 2. You start PostgreSQL but never run `systemctl enable`. What happens after a reboot?

  3. 3. Why does `sudo -u postgres psql` let you in without a password on a fresh Linux install?

  4. 4. Which extra step do RHEL/CentOS installs need that Ubuntu does not?

  5. 5. Can you back up a PostgreSQL 16 server using pg_dump from version 13?

Summary

  • Installing gives you a server, client tools, or both — you do not always need the server locally.
  • On Debian/Ubuntu the data directory is created for you; on RHEL/CentOS run `postgresql-setup --initdb`.
  • `enable` controls starting at boot and is separate from `start`, which only affects right now.
  • Prefer `reload` over `restart`: reload re-reads config without dropping connections.
  • Major versions change the on-disk format; pg_dump must be at least as new as the server.