LoginSignup
17
17

More than 3 years have passed since last update.

PostgreSQLサーバにpsqlコマンドで接続できなくなった場合の対処法

Posted at

PostgreSQLサーバ作成後に突然以下のエラーが発生して、psqlコマンドで接続できなくなったので解決方法をまとめます。

エラー内容
$ psql
psql: サーバに接続できませんでした: そのようなファイルやディレクトリはありません
                              ローカルにサーバが稼動していますか?
                              Unixドメインソケット"/var/run/postgresql/.s.PGSQL.5432"で通信を受け付けていますか?

■環境情報

OS:CentOS 7.8.2003
SW:PostgreSQL12.3
  VirtualBox6.1.14

作業内容

■状況の確認をしてみます。

psqlコマンドを実行するとエラーが出力されます。

$ psql
psql: サーバに接続できませんでした: そのようなファイルやディレクトリはありません
                              ローカルにサーバが稼動していますか?
                              Unixドメインソケット"/var/run/postgresql/.s.PGSQL.5432"で通信を受け付けていますか?

Unixドメインソケットがありそうな場所を確認してみるとディレクトリがそもそもない感じだと思います。

$ ls -l /var/run/postgresql
ls: /var/run/postgresql にアクセスできません: そのようなファイルやディレクトリはありません

多分以下にソケットファイルが存在すると思います。

$ ls -la /tmp/
srwxrwxrwx   1 postgres postgres       0 10月 18 21:31 .s.PGSQL.5432
-rw-------   1 postgres postgres      36 10月 18 21:31 .s.PGSQL.5432.lock

ちなみのソケットファイルの作成場所はpostgreSQL起動時に出力されています。
この場合だと「/tmp/.s.PGSQL.5432」です。


$ pg_ctl -D $PGDATA start
waiting for server to start....2020-10-18 21:20:35.431 JST [1579] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-10-18 21:20:35.431 JST [1579] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-10-18 21:20:35.431 JST [1579] LOG:  listening on IPv6 address "::", port 5432
2020-10-18 21:20:35.433 JST [1579] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-10-18 21:20:35.449 JST [1580] LOG:  database system was shut down at 2020-10-17 01:05:14 JST
2020-10-18 21:20:35.454 JST [1579] LOG:  database system is ready to accept connections
 done
server started

■psqlコマンドでPostgreSQLサーバに接続できるように設定します。

まず、存在しない「var/run/postgresql」ディレクトリを作成しましょう。

mkdirでディレクトリを作成しただけでは、OSを再起動した際にディレクトリが削除されてしまうため、
「/etc/tmpfiles.d」にファイルを作成しOS起動時に自動的に作成されるようにします。

ファイルを作成し、以下の行を記載する。
# vi /etc/tmpfiles.d/postgresql.conf
d /var/run/postgresql 0755 postgres postgres -


設定を反映させます。
# systemd-tmpfiles --create /etc/tmpfiles.d/postgresql.conf 

ディレクトリが作成されたことを確認します。
# ls -la /var/run/postgresql/
drwxr-xr-x  2 postgres postgres  80 10月 18 21:33 .
drwxr-xr-x 25 root     root     760 10月 18 21:31 ..

PostgreSQLの設定ファイルを編集し、ソケットファイルが「/tmp」ではなく、
「/var/run/postgresql」に出力されるようにします。

postgreSQLを実行するユーザに切り替えます。
# su - postgres

$ vi $PGDATA/postgresql.conf
/unix_socketで検索すると対象の行が見つかると思います。

#unix_socket_directories = '/tmp' # comma-separated list of directories
↓ 以下に変更
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories


PostgreSQLサーバを起動します。
ソケットファイルが「/var/run/postgresql/.s.PGSQL.5432」で受け付けられていることを確認します。


$ pg_ctl -D $PGDATA start
waiting for server to start....2020-10-18 21:59:06.590 JST [1781] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-10-18 21:59:06.590 JST [1781] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-10-18 21:59:06.590 JST [1781] LOG:  listening on IPv6 address "::", port 5432
2020-10-18 21:59:06.594 JST [1781] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-10-18 21:59:06.612 JST [1782] LOG:  database system was shut down at 2020-10-18 21:59:00 JST
2020-10-18 21:59:06.614 JST [1781] LOG:  database system is ready to accept connections
 done
server started

ソケットファイル「.s.PGSQL.5432」が作成されたことを確認します。


$ ls -la /var/run/postgresql/
合計 4
drwxr-xr-x  2 postgres postgres  80 10月 18 21:33 .
drwxr-xr-x 25 root     root     760 10月 18 21:31 ..
srwxrwxrwx  1 postgres postgres   0 10月 18 21:33 .s.PGSQL.5432
-rw-------  1 postgres postgres  51 10月 18 21:33 .s.PGSQL.5432.lock

psqlコマンドにてPostgreSQLサーバに接続してみましょう。


$ psql
psql (9.2.24, サーバー 12.3)
注意: psql バージョン 9.2, サーバーバージョン 12.0.
         psql の機能の中で、動作しないものがあるかもしれません。
"help" でヘルプを表示します.

postgres=# 

以上です。

参考URL

再起動後に /var/run/postgresql がなくなっている
https://teratail.com/questions/246340

How to change the default unix socket location from /tmp for PostgreSQL
https://www.manniwood.com/2015_11_14/how_to_change_the_default_unix_socket_location_from_tmp_for_postgresql.html

17
17
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
17
17