PostgreSQL

PostgreSQL サービスや DB のステータスの確認と再起動する方法

ステータス確認方法

バージョンに合わせて以下コマンドの数字を変える

systemctl status postgresql-12.service

実行例
Last login: Tue Mar 14 12:28:51 2023 from 172.24.243.44
[postgres@hellomyworld ~]$ systemctl status postgresql-12.service
● postgresql-12.service - PostgreSQL 12 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-03-14 12:32:44 CDT; 1 weeks 4 days ago
     Docs: https://www.postgresql.org/docs/12/static/
  Process: 4359 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 4374 (postmaster)
    Tasks: 9
   CGroup: /system.slice/postgresql-12.service
           ├─4374 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
           ├─4477 postgres: logger   
           ├─4591 postgres: checkpointer   
           ├─4592 postgres: background writer   
           ├─4593 postgres: walwriter   
           ├─4594 postgres: autovacuum launcher   
           ├─4595 postgres: archiver   
           ├─4596 postgres: stats collector   
           └─4597 postgres: logical replication launcher  

再起動方法

パスワードがわからない時は root 権限があるとコマンド実行が可能になる

systemctl restart postgresql-12.service

実行例
[postgres@hellomyworld ~]$ systemctl restart postgresql-12.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: test
Password: 
polkit-agent-helper-1: pam_authenticate failed: Authentication failure
==== AUTHENTICATION FAILED ===
Failed to restart postgresql-12.service: Access denied
See system logs and 'systemctl status postgresql-12.service' for details.
[postgres@hellomyworld ~]$ su
Password: 

[root@hellomyworld postgres]# systemctl restart postgresql-12.service

状態確認

実行例
[root@hellomyworld postgres]# systemctl status postgresql-12.service
● postgresql-12.service - PostgreSQL 12 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2023-03-25 14:42:24 CDT; 53s ago
     Docs: https://www.postgresql.org/docs/12/static/
  Process: 18854 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 18860 (postmaster)
    Tasks: 9
   CGroup: /system.slice/postgresql-12.service
           ├─18860 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
           ├─18862 postgres: logger   
           ├─18864 postgres: checkpointer   
           ├─18865 postgres: background writer   
           ├─18866 postgres: walwriter   
           ├─18867 postgres: autovacuum launcher   
           ├─18868 postgres: archiver   
           ├─18869 postgres: stats collector   
           └─18870 postgres: logical replication launcher   
 
Mar 25 14:42:24 hellomyworld systemd[1]: Stopped PostgreSQL 12 database server.
Mar 25 14:42:24 hellomyworld systemd[1]: Starting PostgreSQL 12 database server...
Mar 25 14:42:24 hellomyworld postmaster[18860]: 2023-03-25 14:42:24.823 CDT [18860] LOG:  starting PostgreSQL 12.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
Mar 25 14:42:24 hellomyworld postmaster[18860]: 2023-03-25 14:42:24.823 CDT [18860] LOG:  listening on IPv4 address "0.0.0.0", port 5432
Mar 25 14:42:24 hellomyworld postmaster[18860]: 2023-03-25 14:42:24.824 CDT [18860] LOG:  listening on IPv6 address "::", port 5432
Mar 25 14:42:24 hellomyworld postmaster[18860]: 2023-03-25 14:42:24.826 CDT [18860] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Mar 25 14:42:24 hellomyworld postmaster[18860]: 2023-03-25 14:42:24.830 CDT [18860] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
Mar 25 14:42:24 hellomyworld postmaster[18860]: 2023-03-25 14:42:24.843 CDT [18860] LOG:  redirecting log output to logging collector process
Mar 25 14:42:24 hellomyworld postmaster[18860]: 2023-03-25 14:42:24.843 CDT [18860] HINT:  Future log output will appear in directory "log".
Mar 25 14:42:24 hellomyworld systemd[1]: Started PostgreSQL 12 database server.
[root@hellomyworld postgres]#

CLIを使って再起動も可能

pg_ctl を使う方法もある

/usr/pgsql-12/bin/pg_ctl restart

実行例
[postgres@hellomyworld ~]$ /usr/pgsql-12/bin/pg_ctl restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2023-02-15 14:17:12.607 CST [16964] LOG:  starting PostgreSQL 12.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2023-02-15 14:17:12.607 CST [16964] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-02-15 14:17:12.607 CST [16964] LOG:  listening on IPv6 address "::", port 5432
2023-02-15 14:17:12.609 CST [16964] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-02-15 14:17:12.614 CST [16964] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-02-15 14:17:12.629 CST [16964] LOG:  redirecting log output to logging collector process
2023-02-15 14:17:12.629 CST [16964] HINT:  Future log output will appear in directory "log".
done
server started

この記事が役に立ったという方は
ボタンをポチッとしてくれたら喜びます

-PostgreSQL
-,