はじめに
CentOS7ですがpostgresqlをインストール後、postgresqlのサービスを再起動したら失敗したため、事象について記事にしました。
postgresqlの再起動失敗
[root@localhost ~]# systemctl restart postgresql
Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.
詳細のエラーを確認
[root@localhost ~]# journalctl -xe
11月 26 20:23:44 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
-- Subject: Unit postgresql.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit postgresql.service has begun starting up.
11月 26 20:23:44 localhost.localdomain postgresql-check-db-dir[9979]: "/var/lib/pgsql/data" is missing or empty.
11月 26 20:23:44 localhost.localdomain postgresql-check-db-dir[9979]: Use "postgresql-setup initdb" to initialize the database cluster.
11月 26 20:23:44 localhost.localdomain postgresql-check-db-dir[9979]: See /usr/share/doc/postgresql-9.2.24/README.rpm-dist for more informat
11月 26 20:23:44 localhost.localdomain polkitd[652]: Unregistered Authentication Agent for unix-process:9972:400235 (system bus name :1.30,
11月 26 20:23:44 localhost.localdomain systemd[1]: postgresql.service: control process exited, code=exited status=1
11月 26 20:23:44 localhost.localdomain systemd[1]: Failed to start PostgreSQL database server.
-- Subject: Unit postgresql.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit postgresql.service has failed.
--
-- The result is failed.
postgresqlの初期設定するスクリプトを探しました。
以下のサイトを参考にしました。
https://www.postgresql.jp/document/9.4/html/app-initdb.html
https://qiita.com/LowSE01/items/84af05449f96dedd0edc
postgresqlをインストールした後にpostgresqlのデータベースの初期設定が必要になるそうです。
環境によってpostgresqlの初期設定のスクリプトの名前が違う場合があるため、findコマンドで検索しました。
[root@localhost ~]# find / -name postgresql*
/run/postgresql
/sys/fs/selinux/booleans/postgresql_selinux_unconfined_dbadm
/sys/fs/selinux/booleans/postgresql_selinux_transmit_client_label
/sys/fs/selinux/booleans/postgresql_selinux_users_ddl
/sys/fs/selinux/booleans/postgresql_can_rsync
/etc/selinux/targeted/active/modules/100/postgresql
/etc/pam.d/postgresql
/usr/bin/postgresql-check-db-dir
/usr/bin/postgresql-setup ←みつかりました。
/usr/lib/systemd/system/postgresql.service
/usr/lib/tmpfiles.d/postgresql.conf
/usr/lib/firewalld/services/postgresql.xml
/usr/share/doc/postgresql-libs-9.2.24
/usr/share/doc/postgresql-9.2.24
/usr/share/man/man1/postgresql-setup.1.gz
/usr/share/pgsql/postgresql.conf.sample
/usr/libexec/initscripts/legacy-actions/postgresql
postgresql-setupを実行しました。
[root@localhost ~]# postgresql-setup initdb
Initializing database ... OK
postgresqlの再起動が成功しました。
# systemctl restart postgresql
postgresqlのサービスのステータスを確認しました。
[root@localhost ~]# systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
Active: active (running) since 日 2023-11-26 20:28:03 JST; 7s ago
Process: 10280 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=0/SUCCESS)
Process: 10274 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 10283 (postgres)
CGroup: /system.slice/postgresql.service
tq10283 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432
tq10284 postgres: logger process
tq10286 postgres: checkpointer process
tq10287 postgres: writer process
tq10288 postgres: wal writer process
tq10289 postgres: autovacuum launcher process
mq10290 postgres: stats collector process
11月 26 20:28:02 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
11月 26 20:28:03 localhost.localdomain systemd[1]: Started PostgreSQL database server.