Goal: manage a server lifecycle, diagnose a deliberate startup failure, and prove recovery.
Use as-postgres pg_ctl status to inspect the running server PID and launch command. pg_ctl checks the recorded process, not merely whether a PID file exists. A SQL query later checks query-level readiness.
as-postgres runs as the Linux cluster owner. -D selects /var/lib/postgresql/19/data; it is the cluster directory, not the SQL database name. Run this first command at the shell prompt. Expect server is running and a PID that can vary.
Goal: open the SQL connection used for live settings and readiness checks.
Connect as the SQL role postgres. Protected operating-system commands still use as-postgres; the two identities are separate.
psql -U postgres -d postgres selects the SQL role and database. Expect the postgres=# prompt; SQL runs here, while later \! commands run a shell and return to this client.
Goal: know where to read normal logs versus a failed-start log.
Inspect the collector, configured directory, filename, and current file. The collector is on and writes log/postgresql.log. We will use /tmp/service-startup.log for launcher output; parse failures occur before the collector can start.
log_directory is relative to the data directory here, making the full collector path /var/lib/postgresql/19/data/log/postgresql.log. pg_current_logfile() locates collector output, not the separate launcher file selected later with -l.
Goal: send a reload request without replacing the SQL connection.
Stay in psql and run the OS reload command through \!. pg_ctl reload sends SIGHUP; server signaled is acknowledgement of signalling, not proof that every setting applied.
\! runs the operating-system command and then returns to psql. SIGHUP is the server’s reload signal; no configuration edit is made in this step. Expect server signaled and the same usable psql prompt.
Goal: save a baseline that survives stopping and reconnecting.
Save pg_postmaster_start_time() with client-side \copy, then display it. We will compare this file after reconnecting, rather than rely on remembering a timestamp from an earlier lesson.
pg_postmaster_start_time() identifies when the server instance began. \copy exports the query result through this client to /tmp/service-start.txt; COPY 1 means one row written. A file survives closing psql, whereas a client variable would be lost. Expect one exported timestamp and the same timestamp displayed by SQL.
Goal: perform a clean smart shutdown and verify the stopped state.
Exit psql first. Smart shutdown waits for existing sessions to disconnect; with our client gone, it can complete. Then run pg_ctl status: the expected no server is running result confirms the stop. This lab does not measure smart-mode behavior under a busy workload.
\q returns to the shell. -m smart selects the shutdown mode; -w waits for completion. Expect server stopped, then pg_ctl: no server running. The status command’s nonzero exit is expected for a stopped server.
Goal: introduce one identifiable configuration defect for diagnosis.
Append exactly shared_buffers = 256MB oops and read the last line back. The malformed token oops is deliberate; existing configuration lines stay intact.
printf '%s\n' writes the text plus a newline. The pipe passes it to tee -a, which appends as the Linux owner and also prints the line; tail -n 1 reads it back. Expect the bad line twice, proving the append and inspection occurred.
Goal: observe the startup failure caused by that specific malformed line.
Start with waiting enabled and launcher output directed to /tmp/service-startup.log. Expect could not start server. The next step reads the log to identify the deliberate syntax error.
-w waits for startup, and -l directs launcher output to the named file so the failure can be read next. The failure is intentional; do not repeat starts or change unrelated settings before inspecting its log.
Goal: identify the cause of the failed start from its recorded error.
Read the launcher log as the Linux postgres user. Find the named postgresql.conf, its line number, and token oops, plus the fatal configuration-error message. The line number can change across images.
tail -n 10 prints the final ten log lines. Success is finding both the syntax error near oops and the fatal configuration error. A generic startup-failed message alone does not identify the cause.
Goal: remove exactly the defect while retaining the valid setting.
Delete only the complete labelled line, then confirm a valid configured shared_buffers = 16MB remains and print a success marker after checking that the bad line is absent. Broadly deleting every line containing oops could erase unrelated configuration.
sed -i edits in place; ^ and $ anchor the whole line, and d deletes that match. grep -q tests without printing; shell ! reverses its result, and && echo prints the marker only when the bad line is absent. Expect shared_buffers = 16MB and BAD_LINE_REMOVED; an empty sed output alone is insufficient.
Goal: start the repaired cluster and check it accepts connections.
Start again with -w, then use pg_isready on the Unix socket. Waiting pg_ctl start already checks startup readiness; the following SQL step verifies it can answer a real query and that this is a newer server instance.
pg_isready -h /tmp selects the Unix socket directory rather than a TCP hostname. Expect server started and /tmp:5432 - accepting connections. This is connection readiness; the next query checks application-level response.
Goal: verify a newer instance responds with the intended settings.
Reconnect and compare the start timestamp with the saved baseline: restarted true. Read the unchanged active 16MB shared_buffers / 20 max_connections. This verifies recovery from the specific parse error, not every possible production health criterion.
pg_read_file reads the saved file on the server; ::timestamptz converts its text for comparison. The > test must yield t (true), while current_setting reads active values. The shared file is accessible here because client and server run on the same machine.
Goal: complete another clean stop/start cycle using fast shutdown.
Leave psql, perform a clean fast shutdown, check the stopped status, start again, reconnect, and run SELECT 1. Fast mode disconnects clients and aborts active transactions; we demonstrate the stop/start lifecycle here, not transaction cancellation under load. Immediate mode is reference-only in this lab.
Run OS commands at the shell after \q, then reconnect for SQL. Expect server stopped, no server running, server started, and finally query_ready = 1. SELECT 1 is a constant query that proves SQL execution works; it does not count table rows.