Sub topic 3
Installing the server
This chapter covers installing PostgreSQL or MySQL from packages on a Debian or Ubuntu host. It assumes you are comfortable with sudo, apt, and systemd units, and that you know basic SQL. Configuration, networking and security come in later chapters. Here the goal is a running server that you understand well enough to find its pieces.
What a package install gives you
A database server package does more than copy a binary. On Debian and Ubuntu, installing it typically does the following:
- Installs the server binaries, client tools and shared libraries.
- Creates a system account (
postgresormysql) that owns the data and runs the process. - Creates a data directory and initializes it, so the server has a working empty cluster or instance.
- Installs a systemd unit and, on Debian-family systems, starts the service right away.
- Writes default configuration files under
/etc.
Because the package initializes the data directory for you, you do not run initdb or mysqld --initialize yourself in the normal case. Running them against an existing data directory is a common way to damage an install, so avoid it unless you are deliberately building a new instance.
Distribution packages or vendor repositories
You have two sources for packages.
| Source | Advantages | Drawbacks |
|---|---|---|
| Distribution repository | Tested with your release, receives security fixes through normal updates, no extra trust to configure | Major version is fixed for the life of the release, so it may be older than you want |
| Vendor repository (PostgreSQL PGDG, MySQL APT repository) | Newer major versions, several versions side by side (PostgreSQL) | You add a third-party signing key and repository, and you own the upgrade planning |
For most hosts, start with the distribution package. Move to a vendor repository when you need a specific major version, for example to match a production target or to get a feature the distribution version lacks.
Installing PostgreSQL or MySQL
The commands below install the distribution package, start it, enable it at boot, and check its status. The MySQL lines are commented out as an alternative.
# Debian or Ubuntu
sudo apt update
sudo apt install postgresql
sudo systemctl enable --now postgresql
systemctl status postgresql
# Alternative: MySQL on Debian or Ubuntu
# sudo apt install mysql-server
# sudo systemctl enable --now mysqlInstall one engine per learning host unless you want to compare them. Both can run on one machine, because they use different ports (5432 and 3306), but they compete for memory.
What `enable --now` does
systemctl enable makes the service start at boot. The --now flag also starts it immediately. On Debian-family systems the package may already have started and enabled the service, in which case the command changes nothing. It is safe to run either way.
A PostgreSQL detail: the umbrella unit
On Debian and Ubuntu, postgresql.service is an umbrella unit. It does nothing itself and brings up one per-cluster unit, named like [email protected]. The cluster name is main and the number is the major version. The status of postgresql.service can show active (exited), which is normal. To see the real server process, list the units:
systemctl list-units 'postgresql*'
pg_lsclusterspg_lsclusters comes from the Debian postgresql-common package. It prints the version, cluster name, port, status, data directory and log file path for each cluster. Other distributions, such as RHEL and its derivatives, do not provide it and use a single postgresql unit after you run postgresql-setup --initdb.
Verifying the install
Check three things: the version, a working connection, and the listening socket.
- Check the client and server versions.
- Connect through the local socket as the administrative account.
- Confirm the process is listening where you expect.
psql --version
sudo -u postgres psql -c 'SELECT version();'
ss -ltnp | grep -E ':(5432|3306)'For MySQL, use mysql --version and sudo mysql -e 'SELECT VERSION();'.
The local administrative login works without a password in both engines, but for different reasons:
- PostgreSQL uses peer authentication on the Unix socket. The OS user
postgresmaps to the database rolepostgres, so you switch to that OS user withsudo -u postgres. - MySQL on Debian and Ubuntu sets the
rootaccount to authenticate with theauth_socketplugin. Runningsudo mysqlas the OS root user is accepted without a password.
Neither is a network login. This is why a fresh install can be administered locally but is not yet reachable by applications. Later chapters cover that.
Common installation problems
- Package not found. Run
sudo apt updatefirst, and check the spelling of the package name. On a vendor repository, verify that the repository supports your release codename. - Service did not start after install. Look at
sudo systemctl statusfor the unit andsudo journalctl -u postgresqlor-u mysql. Typical causes are a port already in use, a full disk, or a data directory with wrong ownership. - Port already in use. Another instance, possibly the other engine or an older cluster, holds the port.
ss -ltnpshows the owner. Debian assigns the next free port (5433 and so on) to additional PostgreSQL clusters, so checkpg_lsclustersbefore assuming 5432. - Installed a new major version next to an old one. On PostgreSQL, a second major version creates a second cluster on a different port. It does not upgrade the first. Your data stays in the old cluster until you migrate it deliberately.
- Unattended upgrades restart the server. Minor version updates restart the service. Plan for that, and see the maintenance chapter.
Where things end up
After installation, note these locations. The next chapter covers them in depth.
| Item | PostgreSQL (Debian family) | MySQL (Debian family) |
|---|---|---|
| Service account | `postgres` | `mysql` |
| Configuration | `/etc/postgresql/<version>/main/` | `/etc/mysql/` |
| Data directory | `/var/lib/postgresql/<version>/main` | `/var/lib/mysql` |
| Default port | 5432 | 3306 |
Paths differ on other distributions. On RHEL-family systems, PostgreSQL keeps configuration inside the data directory under /var/lib/pgsql/data.
Exercise
After running sudo apt install postgresql on Ubuntu, systemctl status postgresql shows active (exited). Is the database down? How do you check?
Show answer
No. On Debian and Ubuntu, postgresql.service is an umbrella unit that exits after starting the per-cluster units, so active (exited) is normal. Check the real server with pg_lsclusters (the status should be online) or systemctl list-units 'postgresql*' for a unit such as [email protected]. Then confirm the listening socket with ss -ltnp | grep 5432.
Exercise
Why can you run sudo -u postgres psql without a password on a fresh install, and why does that not mean applications can connect over the network?
Show answer
The default local rule uses peer authentication on the Unix socket, which trusts the OS user name and maps it to the database role. It applies only to local socket connections. A fresh install listens on loopback only, and network clients need a host-based rule and a role with a password, which later chapters set up.