Sub topic 4
Starting, stopping and enabling the service
This chapter assumes you can use sudo, have a database package installed (see the previous chapter), and know what a systemd unit is. It covers the systemctl commands that control the database, what each one does to client connections, and how to read the service state when something is wrong.
Running versus enabled
systemd tracks two independent facts about a service:
- Active means the service is running now.
- Enabled means systemd will start it at boot.
A service can be any combination of the two. A freshly installed database on Debian or Ubuntu is usually both, because the package scripts start it and enable it. On Red Hat family systems, packages typically install the server stopped and disabled, so you start it yourself.
| State | Meaning | Typical cause |
|---|---|---|
| active, enabled | Running now and after reboot | Normal production state |
| active, disabled | Running now, gone after reboot | Started by hand, never enabled |
| inactive, enabled | Stopped now, returns after reboot | Stopped for maintenance |
| failed | Tried to start and exited with an error | Bad config, port in use, bad permissions |
| masked | Cannot be started at all | Someone ran `systemctl mask` |
Finding the right unit name
The unit name depends on the engine and the distribution. Check before you type.
| Engine and distribution | Unit name |
|---|---|
| PostgreSQL on Debian or Ubuntu | `postgresql` (a wrapper) and `postgresql@16-main` (one per cluster) |
| PostgreSQL on RHEL family, PGDG packages | `postgresql-16` |
| MySQL on Debian or Ubuntu | `mysql` |
| MySQL on RHEL family | `mysqld` |
The version number in the PostgreSQL names is only an example. List what exists on your host:
systemctl list-unit-files --type=service | grep -Ei 'postgres|mysql|mariadb'On Debian and Ubuntu, one PostgreSQL install can hold several clusters. A cluster is one data directory served by one server process on one port. The postgresql unit starts and stops all of them together. The postgresql@16-main unit controls only the cluster named main of version 16. The pg_lsclusters command shows each cluster's version, name, port, status and data directory.
The everyday commands
sudo systemctl start postgresql # start now
sudo systemctl stop postgresql # stop now
sudo systemctl restart postgresql # stop, then start
sudo systemctl reload postgresql # re-read configuration without stopping
sudo systemctl enable postgresql # start at boot
sudo systemctl disable postgresql # do not start at boot
sudo systemctl enable --now postgresql # enable and start in one step
sudo systemctl disable --now postgresql # disable and stop in one stepReplace postgresql with mysql or mysqld as needed. The state checks are scriptable because they set the exit code:
systemctl is-active postgresql # prints active, inactive or failed
systemctl is-enabled postgresql # prints enabled, disabled or masked
systemctl status postgresql # state, main PID, last log linesstatus shows the last few log lines, which is often enough to see why a start failed. For the full log, use journalctl -u postgresql. Chapter 18 covers reading logs in depth.
Reload versus restart
A restart ends every client connection. Applications see dropped sockets, in-flight transactions roll back, and the server has to warm its caches again. Prefer a reload when the setting allows it.
PostgreSQL. A reload sends the postmaster (the parent server process) a SIGHUP signal. It re-reads postgresql.conf and pg_hba.conf, and the new values apply to new sessions. Changes to listen_addresses, port, shared_buffers and max_connections need a full restart. After editing, ask the server which changes are still waiting:
SELECT name, setting, pending_restart
FROM pg_settings
WHERE pending_restart;If this returns rows, a restart is needed. You can also trigger a reload from SQL with SELECT pg_reload_conf(); as a superuser.
MySQL. Many variables can change at runtime with SET GLOBAL, which takes effect immediately but is lost at restart. SET PERSIST also writes the value to mysqld-auto.cnf so it survives restarts. Settings such as bind-address and port in the option file need a restart. MySQL's systemctl reload does not re-read the option file the way PostgreSQL's does, so do not rely on it for configuration changes.
SET PERSIST max_connections = 300;What a stop does to clients
Stopping a database is not like stopping a stateless web server. It must write its in-memory changes safely to disk first.
PostgreSQL has three shutdown modes:
| Mode | Behavior | When used |
|---|---|---|
| smart | Waits for all clients to disconnect | Rarely practical |
| fast | Rolls back open transactions, disconnects clients, checkpoints, exits | Default for systemd stops |
| immediate | Aborts without a checkpoint | Next start runs crash recovery |
MySQL performs a clean shutdown on SIGTERM. It flushes dirty pages from the InnoDB buffer pool and ends connections. With innodb_fast_shutdown=1 (the default) it skips some purge work. Setting it to 0 forces a slow shutdown that does a full purge, which is sometimes wanted before a major version upgrade.
A large buffer pool or a long checkpoint can make a stop take minutes. systemd's default stop timeout is 90 seconds, after which it sends SIGKILL. A killed database is not corrupted, because both engines use write-ahead logging, but it must run crash recovery on the next start and that delays availability.
Changing unit settings safely
Never edit files under /lib/systemd/system or /usr/lib/systemd/system. A package update overwrites them. Use a drop-in override instead:
sudo systemctl edit mysqlThis opens an editor and saves your text to /etc/systemd/system/mysql.service.d/override.conf. A typical override gives a large instance more time to shut down:
[Service]
TimeoutStopSec=600systemctl reloads the unit definitions when you save. Restart the service for the change to apply. Use systemctl cat mysql to see the unit file and every drop-in merged together, and systemctl show mysql -p TimeoutStopUSec to confirm the active value.
The lifecycle
Common problems
- Start returns at once but the service is down. Run
systemctl statusandjournalctl -u. The usual causes are a syntax error in the configuration file, a wrong owner on the data directory, or another process already holding the port. - Service does not come back after reboot. It was started but never enabled. Check
systemctl is-enabled. Unit is masked. Someone deliberately blocked it. Runsudo systemctl unmask postgresqlonly if you know why it was masked.- Restart loops. If the unit has
Restart=on-failure, systemd retries a broken start. After too many failures it stops withstart-limit-hit. Fix the cause, then runsudo systemctl reset-failed postgresql. - Debian wrapper confusion.
systemctl status postgresqlcan showactive (exited)even when one cluster is down. The wrapper unit only runs the per-cluster starts. Checkpg_lsclustersor thepostgresql@16-mainunit. - Connections succeed from the shell but not the app. The service is running, but it may be listening on a different address. Verify with
ss -ltnp, as covered in the next chapters.
Exercise
You edit pg_hba.conf to allow a new application server and also change max_connections in postgresql.conf. Which systemctl command do you run to apply both, and how do you tell whether a restart is required?
Show answer
Run sudo systemctl reload postgresql first. It applies the pg_hba.conf change to new sessions without dropping connections. max_connections only takes effect after a full restart, so query SELECT name, pending_restart FROM pg_settings WHERE pending_restart;. If it returns max_connections, schedule sudo systemctl restart postgresql for a maintenance window.
Exercise
After a reboot, the database is down. systemctl is-active postgresql prints inactive and systemctl is-enabled postgresql prints disabled. What single command fixes both, and why is that better than running start alone?
Show answer
sudo systemctl enable --now postgresql. It starts the service immediately and enables it for future boots. Running start alone would fix the current outage but leave the service disabled, so the next reboot would repeat the problem.