PostgreSQL

PostgreSQL 再起動・起動確認・停止・起動のコマンド(Linux の systemctl)

Linux(systemd)の PostgreSQL は、systemctl で起動・停止・再起動・状態確認をします。たとえば再起動は systemctl restart postgresql-18、起動しているかの確認は systemctl status postgresql-18 です。サービス名は入れ方で違い、PostgreSQL 公式のパッケージ(PGDG)なら postgresql-18 のように版の番号付き、OS のパッケージなら postgresql です。どれも root(または sudo を付けて)実行します。

コマンドのまとめ

したいことコマンド
起動systemctl start postgresql-18
停止systemctl stop postgresql-18
再起動systemctl restart postgresql-18
設定ファイルの再読み込み(止めずに反映)systemctl reload postgresql-18
状態の確認systemctl status postgresql-18
起動しているかだけ確認systemctl is-active postgresql-18
OS の起動時に自動で起動systemctl enable postgresql-18

サービス名を確かめる

サービス名が分からないときは、postgresql で始まるサービスを一覧します。

# systemctl list-unit-files 'postgresql*'
UNIT FILE             STATE    PRESET
postgresql-18.service disabled disabled

1 unit files listed.

起動しているか確認する(status・is-active・pg_isready)

Active が active (running) なら起動しています。スクリプトで判定するなら、active か inactive だけを返す is-active が便利です。サービスが動いていても接続を受け付けられるとは限らないので、接続できるかまで確かめるなら、PostgreSQL に付いている pg_isready を使います。

# systemctl status postgresql-18
● postgresql-18.service - PostgreSQL 18 database server
     Loaded: loaded (/usr/lib/systemd/system/postgresql-18.service; disabled; preset: disabled)
     Active: active (running) since Fri 2026-11-20 11:12:40 UTC; 10ms ago
       Docs: https://www.postgresql.org/docs/18/static/
    Process: 573 ExecStartPre=/usr/pgsql-18/bin/postgresql-18-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
   Main PID: 578 (postgres)
      Tasks: 10 (limit: 8569)
     Memory: 23.1M (peak: 23.1M)
        CPU: 47ms
(以下省略)

# systemctl is-active postgresql-18
active

# 接続を受け付けているかは pg_isready で確かめる
# /usr/pgsql-18/bin/pg_isready
/run/postgresql:5432 - accepting connections

再起動(restart)と、設定の再読み込み(reload)

restart は PostgreSQL をいったん止めるので、接続中のセッションはすべて切れます。pg_hba.conf の変更や、多くの設定(postgresql.conf)は、止めずに反映できる reload で十分です。SQL から select pg_reload_conf(); としても同じです。

shared_buffers・max_connections・port・listen_addresses など、再起動しないと反映されない設定もあります。どれが該当するかは、pg_settings の context が postmaster の設定で確かめられます(「pg_settings ビュー」)。

systemctl reload postgresql-18     # 止めずに設定を読み直す
systemctl restart postgresql-18    # 止めてから起動し直す

停止する

停止すると、接続中のセッションは切られ、途中のトランザクションはロールバックされます。

# systemctl stop postgresql-18
# systemctl is-active postgresql-18
inactive
# /usr/pgsql-18/bin/pg_isready
/run/postgresql:5432 - no response

OS の起動時に自動で起動する

インストールしただけでは、自動起動は無効(disabled)です。enable で有効にします。

# systemctl enable postgresql-18
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-18.service → /usr/lib/systemd/system/postgresql-18.service.
# systemctl is-enabled postgresql-18
enabled

service コマンドと pg_ctl

  • service コマンドでも操作できますが、中では systemctl が呼ばれます。書く順番は service サービス名 操作 です。
  • pg_ctl でも起動・停止できますが、root では実行できず、postgres ユーザーで実行します。systemd で起動した PostgreSQL を pg_ctl で止めると、systemd の状態とずれるので、systemd で動かしているなら systemctl にそろえるのがおすすめです。
# service postgresql-18 status
Redirecting to /bin/systemctl status postgresql-18.service
● postgresql-18.service - PostgreSQL 18 database server
(以下省略)

# 書く順番を逆にすると、エラーになる
# service stop postgresql-18
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, reload-or-restart, try-reload-or-restart, force-reload, status, condrestart). For other actions, please try to use systemctl.

# pg_ctl は root では実行できない
# /usr/pgsql-18/bin/pg_ctl status -D /var/lib/pgsql/18/data
pg_ctl: cannot be run as root

例は Rocky Linux 9 と PostgreSQL 18.7(PostgreSQL 公式のパッケージ)で実行しています。

スポンサーリンク
タイトルとURLをコピーしました