Sub topic 1
How a database server works
Prerequisites
You should be comfortable with the Linux command line and running commands with sudo. You should know basic SQL, including CREATE, SELECT, INSERT, UPDATE and DELETE. You should understand IP addresses, ports, and the difference between localhost and a network interface. You should also know systemd and your distribution's package manager.
How the server fits together
A database server is a long-running process, or daemon, that owns a data directory on disk. Clients such as applications and the psql or mysql command-line tools connect over the network or a local socket. They send SQL and receive results. Only the server process reads and writes the data files.
Choosing between PostgreSQL and MySQL
Both engines are mature and suit most web workloads. Choose the one your application driver and your team already know.
| Factor | PostgreSQL | MySQL |
|---|---|---|
| Main config file | postgresql.conf and pg_hba.conf | my.cnf style files under /etc/mysql |
| Account model | Roles that can log in | Users identified by name and host pattern |
| Strength | Advanced SQL features and strict standards compliance | Simple setup and wide hosting support |
Installing the server
On Debian or Ubuntu, install PostgreSQL from the distribution repository:
sudo apt update
sudo apt install postgresqlOther distributions use different package names, so check your repository first.
Starting, stopping and enabling the service
Use systemctl to control the service:
sudo systemctl enable --now postgresql
systemctl status postgresql
sudo systemctl stop postgresqlThe enable --now form starts the service and enables it at boot.
> Do not add a line like the one shown. Instead, use the following note.
Where configuration and data live
On Debian and Ubuntu, PostgreSQL keeps its configuration in /etc/postgresql/<version>/main/ and its data in /var/lib/postgresql/<version>/main/. Confirm the paths from a connected session with SHOW config_file; and SHOW data_directory;.
Controlling which addresses the server listens on
The listen_addresses setting decides which network interfaces accept connections. Bind only to the addresses you need:
# postgresql.conf
listen_addresses = 'localhost,10.0.0.5'
port = 5432Listening on an address does not grant access. The pg_hba.conf file then decides which clients may connect.
Exercise
Why does changing listen_addresses need a restart rather than a reload?
Show answer
The listening socket is created when the server starts. A reload re-reads only settings that can change while the server runs, so PostgreSQL must recreate the socket with a restart.