LoginSignup
21
20

More than 1 year has passed since last update.

PostgreSQLでcould not start serverと言われた時の対処法

Last updated at Posted at 2020-04-24

はじめに

この記事では、PostgreSQLのサーバーを起動するときに

$ pg_ctl start

waiting for server to start....2020-04-23 14:29:14.708 JST [888] LOG:  starting PostgreSQL 12.2 on x86_64-apple-darwin18.7.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit
2020-04-23 14:29:14.709 JST [888] LOG:  could not bind IPv6 address "::1": Address already in use
2020-04-23 14:29:14.709 JST [888] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2020-04-23 14:29:14.710 JST [888] LOG:  could not bind IPv4 address "127.0.0.1": Address already in use
2020-04-23 14:29:14.710 JST [888] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2020-04-23 14:29:14.710 JST [888] WARNING:  could not create listen socket for "localhost"
2020-04-23 14:29:14.710 JST [888] FATAL:  could not create any TCP/IP sockets
2020-04-23 14:29:14.710 JST [888] LOG:  database system is shut down
 stopped waiting
pg_ctl: could not start server

のようなエラーがでた時の対処法を書きます。

エラーの意味

あのエラーの意味は、「ポート番号5432はすでにもう使われているから、もう5432にはサーバーを立てられないよ!(1行目参照)」です。

私の場合は昨日使っていたサーバーが残っています。

私は、PostgreSQLのサーバーを立ち上げる時、毎回ターミナルで
$ pg_ctl startを打ち、閉じる時は
$ pg_ctl stopを打って閉じています(自動化してくれるような方法があれば教えて欲しい...)

ただ、stopをし忘れたままPCをシャットダウンしてしまう時があり、そうなると冒頭に書いたようなエラーが出ます。
たまにエラーにハマって時間を取られてしまうわけですが、今回、おそらく二度と悩まずに済むような方法を見つけたので、ここに書きます。

試したこと(途中で解決する可能性もあり)

念の為、PostgreSQLにサーバーが立ち上がっているかどうかを確認します。

$ pg_ctl status
pg_ctl: no server running

立ち上がっていませんね。

次にポート5432に何か立ち上がっているのかを見ます。

$ sudo lsof -i:5432
Password:
COMMAND  PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres  65 postgres    4u  IPv6 0x7e9b8bc85f43493d      0t0  TCP *:postgresql (LISTEN)
postgres  65 postgres    5u  IPv4 0x7e9b8bc85eeb7cdd      0t0  TCP *:postgresql (LISTEN)

いました。

つまり、プロセスが残っているので、プロセスを消さねばなりません。プロセスを消すには、PIDファイルを消すことで完了するパターンが多いです。

$ rm /usr/local/var/postgres/postmaster.pid

これを実行してみてください。人によってはこれで解決です。ただし

No such file or directory

と言われた場合は読み進めてください。

postmaster.pidを見つける

ここからは割と地道です。

/usr/local/var/postgresにpostmaster.pidのファイルがない場合は、別のPostgreSQLフォルダの中にあるdataフォルダに入っている可能性があります。

私の場合ですが、このdataフォルダは管理者制限でロックがかかっていたので、ファイルパスを使うことは出来ませんでした。おそらく他の方もその可能性があるので、自力でファイルを探してみます。

1. dataファイルを見つける
Finderを開き、Mac SSD⁩ ▸ ⁨ライブラリ⁩ ▸ ⁨PostgreSQL⁩ ▸ ⁨12⁩(ここはバージョンによって数字が違う)へと進みます。

そうすると、中にdataと言うファイルがありますが、管理者制限でロックがかかっていると思います。

2. ロックを解除します。
フォルダをクリックし、 情報を見る ▸ 右下の南京錠マークをクリックしロック解除 ▸ パソコンのパスワード入力 ▸ everyoneのところが「アクセス不可」となっていると思うので、「読み/書き」に変更する

これでdataフォルダが開けるようになったと思います。
3. そしてその中にpostmaster.pidがあれば、ゴミ箱へ入れる。

4. パソコンを再起動

5. サーバーが起動するか確かめる⬇︎

$ pg_ctl start
waiting for server to start....2020-04-24 10:13:39.361 JST [629] LOG:  starting PostgreSQL 12.2 on x86_64-apple-darwin18.7.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit
2020-04-24 10:13:39.362 JST [629] LOG:  listening on IPv6 address "::1", port 5432
2020-04-24 10:13:39.362 JST [629] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-04-24 10:13:39.363 JST [629] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-04-24 10:13:39.405 JST [630] LOG:  database system was shut down at 2020-04-23 22:25:29 JST
2020-04-24 10:13:39.418 JST [629] LOG:  database system is ready to accept connections
 done
server started

こう表示されたら完了です。お疲れ様でした。

21
20
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
21
20