Postgresqlの起動時のエラーと解決方法

とある日のイヌガジェ

イヌガジェ
イヌガジェ

postgresqlの検証がしたいな~

vagrantで仮想環境立ててdnfでインストールして起動っと……
アレ!?!?

[root@almalinux9-test03 ~]# systemctl enable --now postgresql
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
Job for postgresql.service failed because the control process exited with error code.
See "systemctl status postgresql.service" and "journalctl -xeu postgresql.service" for details.

[root@almalinux9-test03 ~]# systemctl status postgresql.service
× postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2023-08-02 12:16:37 JST; 33s ago
Process: 4050 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=1/FAILURE)
CPU: 4ms

Aug 02 12:16:37 almalinux9-test03 systemd[1]: Starting PostgreSQL database server...
Aug 02 12:16:37 almalinux9-test03 postgresql-check-db-dir[4050]: Directory "/var/lib/pgsql/data" is missing or empty.
Aug 02 12:16:37 almalinux9-test03 postgresql-check-db-dir[4050]: Use "/usr/bin/postgresql-setup --initdb"
Aug 02 12:16:37 almalinux9-test03 postgresql-check-db-dir[4050]: to initialize the database cluster.
Aug 02 12:16:37 almalinux9-test03 postgresql-check-db-dir[4050]: See /usr/share/doc/postgresql/README.rpm-dist for more information.
Aug 02 12:16:37 almalinux9-test03 systemd[1]: postgresql.service: Control process exited, code=exited, status=1/FAILURE
Aug 02 12:16:37 almalinux9-test03 systemd[1]: postgresql.service: Failed with result 'exit-code'.
Aug 02 12:16:37 almalinux9-test03 systemd[1]: Failed to start PostgreSQL database server.
イヌガジェ
イヌガジェ

なんかいきなりエラー出てるやん…

解決方法

解決方法は何通りかあるのですが、
大体は前のプロセスが生きててかぶってるかポートが重複してるか、ディレクトリがないかです。

まずはjournalctlでエラー内容の確認

dataディレクトリない場合

[root@almalinux9-test03 ~]# journalctl -xeu postgresql.service
░░ Support: https://access.redhat.com/support
░░
░░ A start job for unit postgresql.service has finished with a failure.
░░
░░ The job identifier is 1472 and the job result is failed.
Aug 02 12:20:07 almalinux9-test03 systemd[1]: Starting PostgreSQL database server...
░░ Subject: A start job for unit postgresql.service has begun execution
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ A start job for unit postgresql.service has begun execution.
░░
░░ The job identifier is 1762.
Aug 02 12:20:07 almalinux9-test03 postgresql-check-db-dir[4498]: Directory "/var/lib/pgsql/data" is missing or empty.
Aug 02 12:20:07 almalinux9-test03 postgresql-check-db-dir[4498]: Use "/usr/bin/postgresql-setup --initdb"
Aug 02 12:20:07 almalinux9-test03 postgresql-check-db-dir[4498]: to initialize the database cluster.
Aug 02 12:20:07 almalinux9-test03 postgresql-check-db-dir[4498]: See /usr/share/doc/postgresql/README.rpm-dist for more information.
Aug 02 12:20:07 almalinux9-test03 systemd[1]: postgresql.service: Control process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ An ExecStartPre= process belonging to unit postgresql.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
Aug 02 12:20:07 almalinux9-test03 systemd[1]: postgresql.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ The unit postgresql.service has entered the 'failed' state with result 'exit-code'.
Aug 02 12:20:07 almalinux9-test03 systemd[1]: Failed to start PostgreSQL database server.
░░ Subject: A start job for unit postgresql.service has failed
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ A start job for unit postgresql.service has finished with a failure.
░░
░░ The job identifier is 1762 and the job result is failed.

この場合だったらDirectory “/var/lib/pgsql/data” is missing or empty.とある通り、データディレクトリがないのでusr/bin/postgresql-setup –initdbしてあげてください

[root@almalinux9-test03 ~]# postgresql-setup initdb
Initializing database ... mkdir: cannot create directory ‘/var/lib/pgsql/data/pg_log’: File exists 
failed, see /var/lib/pgsql/initdb.log

これでもエラー出たりするので

[root@almalinux9-test03 ~]# tail /var/lib/pgsql/initdb.log
This user must also own the server process. 
The database cluster will be initialized with locale "en_US.UTF-8". 
The default database encoding has accordingly been set to "UTF8". 
The default text search configuration will be set to "english".

initdb: directory "/var/lib/pgsql/data" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/var/lib/pgsql/data" or run initdb with an argument other than "/var/lib/pgsql/data".

指示された通りに /var/lib/pgsql/data を消してからpostgresql-setup initdbをしてPostgresqlを起動すればOKです。

[root@almalinux9-test03 ~]# rm -rf /var/lib/pgsql/data
[root@almalinux9-test03 ~]# postgresql-setup initdb
Initializing database ... OK
[root@almalinux9-test03 ~]# systemctl start postgresql.service

ポートやプロセス重複の場合

[root@almalinux9-test03 ~]# journalctl -xeu postgresql.service
░░ Subject: A start job for unit postgresql.service has failed
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ A start job for unit postgresql.service has finished with a failure.
░░
░░ The job identifier is 1762 and the job result is failed.
Aug 02 12:23:07 almalinux9-test03 systemd[1]: Starting PostgreSQL database server...
░░ Subject: A start job for unit postgresql.service has begun execution
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ A start job for unit postgresql.service has begun execution.
░░
░░ The job identifier is 1884.
Aug 02 12:23:07 almalinux9-test03 postmaster[4582]: 2023-08-02 12:23:07.792 JST [4582] FATAL: lock file "postmaster.pid" already exists
Aug 02 12:23:07 almalinux9-test03 postmaster[4582]: 2023-08-02 12:23:07.792 JST [4582] HINT: Is another postmaster (PID 4566) running in data directory "/>
Aug 02 12:23:07 almalinux9-test03 systemd[1]: postgresql.service: Main process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ An ExecStart= process belonging to unit postgresql.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
Aug 02 12:23:07 almalinux9-test03 systemd[1]: postgresql.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ The unit postgresql.service has entered the 'failed' state with result 'exit-code'.
Aug 02 12:23:07 almalinux9-test03 systemd[1]: Failed to start PostgreSQL database server.
░░ Subject: A start job for unit postgresql.service has failed
░░ Defined-By: systemd
░░ Support: https://access.redhat.com/support
░░
░░ A start job for unit postgresql.service has finished with a failure.
░░
░░ The job identifier is 1884 and the job result is failed.

これもすごく分かりやすいですね。
HINT: Is another postmaster (PID 4566) running in data directory "と書いてるので別のプロセスさんが奪っちゃってるんです。
こういう時はプロセスから絞って確認してあげてKillしてあげてください

[root@almalinux9-test03 ~]# ps auxwwf | grep 4566
postgres 4566 0.0 2.8 208524 27504 ? Ss 12:22 0:00 /usr/bin/postgres -D /var/lib/pgsql/data

やはり、、、〇ね!!!!

[root@almalinux9-test03 ~]# kill -9 4566
[root@almalinux9-test03 ~]# systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-08-02 12:25:35 JST; 6s ago
Process: 4593 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
Main PID: 4595 (postmaster)
Tasks: 8 (limit: 5629)
Memory: 16.9M
CPU: 58ms
CGroup: /system.slice/postgresql.service
├─4595 /usr/bin/postmaster -D /var/lib/pgsql/data
├─4596 "postgres: logger "
├─4598 "postgres: checkpointer "
├─4599 "postgres: background writer "
├─4600 "postgres: walwriter "
├─4601 "postgres: autovacuum launcher "
├─4602 "postgres: stats collector "
└─4603 "postgres: logical replication launcher "

Aug 02 12:25:34 almalinux9-test03 systemd[1]: Starting PostgreSQL database server...
Aug 02 12:25:34 almalinux9-test03 postmaster[4595]: 2023-08-02 12:25:34.464 JST [4595] LOG: redirecting log output to logging collector process
Aug 02 12:25:34 almalinux9-test03 postmaster[4595]: 2023-08-02 12:25:34.464 JST [4595] HINT: Future log output will appear in directory "log".
Aug 02 12:25:35 almalinux9-test03 systemd[1]: Started PostgreSQL database server.

これで一見落着。
みなさんも良いPostgresqlライフを。

コメント

タイトルとURLをコピーしました