Sub topic 4
Starting, stopping and enabling the database service
Assume you know the Linux command line, can use sudo, and understand basic systemd services. Systemd is Linux's service manager. It starts processes, stops them, restarts them after failures when configured, and records their status.
Understand the service lifecycle
A database server runs as a background process. Applications connect to that process through a local socket or a TCP port. Systemd controls the process through a unit, which is a configuration object describing how a service runs.
These commands have different effects:
startruns a stopped service.stopterminates a running service.restartstops and starts the service again.reloadasks the service to reread configuration without fully stopping.enableconfigures the service to start during boot.disableremoves that boot-time setting.statusshows whether the service is running and includes recent log messages.
Starting a service does not normally enable it at boot. Use enable --now when you want both actions.
Start and inspect the service
On Debian or Ubuntu, the PostgreSQL unit is usually named postgresql. MySQL commonly uses mysql. Other distributions may use a versioned PostgreSQL unit such as postgresql-16 or postgresql@16-main.
# PostgreSQL on Debian or Ubuntu
sudo systemctl enable --now postgresql
systemctl is-enabled postgresql
systemctl is-active postgresql
systemctl status postgresql --no-pager
# MySQL instead
# sudo systemctl enable --now mysql
# systemctl status mysql --no-pageris-enabled checks the boot setting. is-active checks the current runtime state. A service can be enabled but inactive if it failed after boot, so check both when diagnosing a problem.
Use restart after a configuration change that requires a full service restart. Prefer reload when the database supports it and the change can be applied without interrupting clients.
sudo systemctl reload postgresql
sudo systemctl restart postgresql
# Stop it temporarily, without changing its boot setting
sudo systemctl stop postgresql
# Start it again
sudo systemctl start postgresqlRead logs and check dependencies
Systemd's journal stores service output and startup failures. The -u option selects a unit. The time filter limits the result to recent events.
sudo journalctl -u postgresql --since "15 minutes ago" --no-pager
sudo systemctl list-dependencies postgresqlIf the service fails, read the first useful error. Common causes include an invalid configuration value, an occupied port, incorrect ownership of the data directory, or insufficient disk space.
Verify the process and port
A running unit does not prove that clients can connect. Confirm that a database process is listening on the expected port.
ss -ltnp | grep -E ':(5432|3306)\b'PostgreSQL normally uses port 5432, and MySQL normally uses port 3306. If no matching line appears, inspect the service status and logs before changing firewall rules.
Exercise
You changed a database setting and want it to start automatically after reboot while applying the change immediately. Which command should you run, and what should you verify?
Show answer
Run sudo systemctl enable --now postgresql, then verify with systemctl is-enabled postgresql, systemctl is-active postgresql, and systemctl status postgresql.