LoginSignup
0
0

PostgreSQLのエラー確認ポイント

Posted at

検証環境

 CentOS8, Postgres12.9 で確認

目的

postgresの検証中に出たエラー対応のまとめです。
もしかしたら誤った内容があるかもしれません。

データベースクラスタ(初期化)

◆エラー
initdb: エラー: ディレクトリ"/var/lib/pgsql/data"は存在しますが、空ではありません~
◆内容
 データファイルがあるとエラーになる。
◆対応
 ・ディレクトリを削除する
 ・既存のディレクトリ名を変更する
 ・作成するディレクトリに別の場所を指定する など

データベースの起動、停止

例1:データベースクラスタが見つからない場合

規定値のディレクトリをリネームしてテストする
本来の値:/var/lib/pgsql/data

変更後:/var/lib/pgsql/data1

にしてテストする。

rootユーザにてsystemctlで起動してみる。

[root@localhost ~]# systemctl start postgresql.service
Job for postgresql.service failed because the control process exited with error code.
See "systemctl status postgresql.service" and "journalctl -xe" for details.

エラー内容はjournalctlを確認するようにと言われる

[root@localhost ~]# journalctl -xe
-- Unit postgresql.service has begun starting up.
 6月 05 10:46:39 localhost.localdomain postgresql-check-db-dir[188431]: Directory "/var/lib/pgsql/data" is missing or empty.

/var/lib/pgsql/dataが見つかりませんと出る。

pg_ctlで起動してみる

[postgres@localhost ~]$ pg_ctl start
pg_ctl: ディレクトリ "/var/lib/pgsql/data" は存在しません

こちらは日本語でエラーが返ってくるのでわかりやすい。

対応

データベースクラスタが見つからない場合は、以下の対応が考えられる
1.initdbでデータベースクラスタを作成する
2.データベースクラスタを指定して起動する
 上記の場合だと「 pg_ctl start -D /var/lib/pgsql/data1」など
 ただし、停止する場合もディレクトリ指定が必要です。

例2:データベースクラスタにアクセスできない

データベースクラスタの権限をrootにしてpostgresで起動する

[root@localhost ~]# chown root:root /var/lib/pgsql/data
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ pg_ctl start
pg_ctl: PIDファイル"/var/lib/pgsql/data/postmaster.pid"をオープンできませんでした: 許可がありません

postgresユーザからアクセスできないのでエラーなる。

#### 対応

・所有者とグループはpostgresにする
chown -R postgres:postgres /var/lib/pgsql

・権限は0750にする。
chmod -R 750 /var/lib/pgsql

クライアントから接続できない

ケース1:postgresql.conf

 listen_addressesで許可されているか

★対応:接続元を許可する
 例
 listen_addresses = '*' 
 
 ※listen_addressesは範囲指定できません。個々の指定はpg_hba.confで行う

### ケース2:pg_hba.conf
 接続元のデータベース、ユーザ、IPアドレス等は許可されているか?

★設定例
例:192.168.11.1でパスワード(md5)認証する場合
host all all 192.168.11.1/32 md5

例:192.168.11配下でパスワード(md5)認証する場合※サブネットマスク 24とする
host all all 192.168.121.0/24 md5

例:レプリケーションの指定でパスワードなしの場合
host replication rep_user 192.168.121.123/32 trust

SQLの結果が帰ってこない

レプリケーションの検証時にハマった

synchronous_standby_namesが有効かつ
synchronous_commitがoff以外かつ
スタンバイサーバが停止している場合、
スタンバイサーバからの応答が無いため、結果が返ってこない

★対応
・スタンバイを稼働させる
もしくは
・synchronous_commitをoff,local以外に設定する
 ただし同期が不安定になるので推奨

データベースが停止できない

起動時のデータディレクトリを環境変数以外にしている場合はエラーになる。
stopコマンドでデータディレクトリを指定する

つまづいたときに調査するポイント

1.OSのジャーナルログ
 journalctl -xe
 ※systemd-journaldが収集したログを表示するためのコマンド

2.postgresのログ
 data/log

3.プロセス確認
 ps -ef | grep postgres

4.プログラムの場所(ディレクトリ、ユーザコマンドの場所)を確認する
 pg_config --bindir

5.環境変数確認
echo $PATH

6.データフォルダ確認
psql で接続して
  SHOW data_directory; を実行する

[postgres@localhost ~]$ psql
psql (12.9)
"help"でヘルプを表示します。
postgres=#  SHOW data_directory;
   data_directory
---------------------
 /var/lib/pgsql/data
(1 行)

7.postgresのバージョン確認
 psql --version

8.systemctlで起動時は ステータス確認でプログラムの場所、データディレクトリは確認できる

[root@localhost ~]# systemctl status postgresql.service
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor >
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor >
   Active: active (running) since Thu 2023-06-08 23:10:01 JST; 6min ago
 Main PID: 1060 (postmaster)
    Tasks: 8 (limit: 11095)
   Memory: 24.5M
   CGroup: /system.slice/postgresql.service
           tq1060 /usr/bin/postmaster -D /var/lib/pgsql/data
           tq1094 postgres: logger
           tq1098 postgres: checkpointer
           tq1099 postgres: background writer
           tq1100 postgres: walwriter
           tq1101 postgres: autovacuum launcher
           tq1102 postgres: stats collector
           mq1103 postgres: logical replication launcher

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0