Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Installation, Setup and Configuring  ›  Lesson

Starting and Stopping the Server

Using `pg_ctl` 12 minControl the server with the tool that started it, whether systemd, pg_ctlcluster or pg_ctl, prefer reload over restart, and use fast shutdown by default.

Starting and Stopping the Server

Pedal needs to restart its server to change a memory setting. One engineer uses systemctl, another runs pg_ctl by hand, and afterwards systemd insists PostgreSQL is "failed" while it is clearly running. There are three tools for the same job, and the rule for choosing is simple.

The rule

Start and stop it with whatever started it; stop fast, never immediate by habit.

pg_ctl is PostgreSQL's own control program. It works on a data directory and must run as the postgres OS user:

pg_ctl -D /var/lib/pgsql/17/data status
pg_ctl -D /var/lib/pgsql/17/data restart -m fast
pg_ctl -D /var/lib/pgsql/17/data reload

systemd is how packaged installs run PostgreSQL as a service that starts at boot. The unit calls the right binaries for you:

sudo systemctl status postgresql-17            # RHEL family
sudo systemctl restart postgresql@17-main      # Debian / Ubuntu
sudo systemctl reload postgresql@17-main       # re-read config, no restart

pg_ctlcluster is the Debian and Ubuntu wrapper that knows about several versions and clusters on one machine. pg_lsclusters lists them:

pg_lsclusters
sudo pg_ctlcluster 17 main restart

If the service manager started the server, stopping it behind systemd's back leaves systemd with a wrong picture, which is exactly what confused Pedal's engineers.

Shutdown modes (-m in pg_ctl) decide what happens to connected clients:

  • smart: wait for every client to disconnect. It can wait forever.
  • fast (the default): roll back open transactions, disconnect clients, write a checkpoint, stop cleanly.
  • immediate: stop at once without a checkpoint. The next start runs crash recovery from the WAL. Use it only in emergencies.

Reload versus restart: a reload re-reads the configuration files and applies settings that allow it, without dropping a single connection. A restart is needed only for settings such as shared_buffers or listen_addresses.

The shortcut

Ask two questions: "What started it?" picks the tool, and "Does this change need a restart?" picks reload or restart.

Changing shared_buffers on Pedal's Ubuntu serverStep 1 of 4
  1. What started it?

    Packages and systemd, so use systemctl (or pg_ctlcluster, which cooperates with it).

The trap is reaching for a restart when a reload would do. Every restart disconnects every client and empties PostgreSQL's own buffer cache, so the first minutes afterwards are slower. Check first; many settings, including most logging ones, apply with a reload. And if the server will not start after a change, look in the server log or journalctl -u for the unit: PostgreSQL names the bad setting in its last lines.

Drills

Answer fast — trust the rule.

quick drill

Which shutdown mode is pg_ctl's default?

quick drill

What happens on the next start after an immediate shutdown?

quick drill

You changed log_min_duration_statement. What is the least disruptive way to apply it?

quick drill

On Ubuntu, which command lists every PostgreSQL cluster and its status?

Remember

  • Start and stop with whatever started it; fast is the normal shutdown.
  • Reload for most settings; restart only for ones such as shared_buffers.
  • Immediate shutdown means crash recovery on the next start.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPlan a restart

Pedal's Ubuntu server needs max_connections raised. Explain which tool you would use, whether a reload is enough, which shutdown mode happens and what clients will notice.