WSL 上の postgresql が起動しなくなった

原因: apt で自動インストールされた次期バージョンの postgres が 5433/tcp を占用したので、別の WSL distro の postgres のポート指定と干渉してた
教訓: postgresql がある環境で 5433/tcp を常用しないほうがよさそう

あらすじ

以下の構成で mastodon と misskey の開発環境を WSL 上に共存させている。
pg (postgresql) は両者とも 16。

+----------------- localhost -----------------+
|                                             |
|    +--------------- WSL ---------------+    |
|    |                                   |    |
|    | +--------------+ +--------------+ |    |
|    | | ubuntu-mstdn | | ubuntu-misky | |    |
|    | |              | |              | |    |
|    | | ap: 3000/tcp | | ap: 3001/tcp | |    |
|    | | pg: 5432/tcp | | pg: 5433/tcp | |    |
|    | +--------------+ +--------------+ |    |
|    +-----------------------------------+    |
|                                             |
+---------------------------------------------+

あるとき misskey の環境を立ち上げようとしたら DB に接続できない旨のエラーが出た。

psql で確認してみると、たしかにつながらない。

misskey@ubuntu-misky:~$ psql --port 5433
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5433" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?

investigating

サービスは起動しているのかな?

root@ubuntu-misky:~# systemctl -a | grep postg
postgresql.service            loaded    inactive   dead            PostgreSQL RDBMS
postgresql@16-main.service    loaded    activating start     start PostgreSQL Cluster 16-main
system-postgresql.slice       loaded    active     active          system-postgresql.slice

してなさそう。
こういうときの原因ってうっかりポートを干渉させてるとか、アップデートでなにかが変わったとかか?

ポートの状態を見てみると、なぞのなにかが 5433/tcp を占用しているところを見つけた。
5432/tcp は隣の distro である ubuntu-mstdn の postgres だ。WSL は netstat にほかの distro の LISTEN も上がってくる*1

root@ubuntu-misky:~# netstat -pantu
tcp        0      0 127.0.0.1:5433          0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -

5433/tcp を使っているプロセスが知りたいけれど netstat -pa にも lsof にも出ない。すわ爆破してつくりなおしか。

そういえばさっき ubuntu-mstdn で apt upgrade したときにやたら時間がかかっていたのを思い出したので、ubuntu-mstdn に移って apt list を調べてみた。

root@ubuntu-mstdn:~# apt list --installed | grep postg
postgresql-16/focal-pgdg,now 16.4-1.pgdg20.04+2 amd64 [installed,automatic]
postgresql-17/focal-pgdg,now 17.0-1.pgdg20.04+1 amd64 [installed,automatic]

postgresql-17 こんにちは、はじめまして。

root@ubuntu-mstdn:~# netstat -pantu
tcp        0      0 127.0.0.1:5433          0.0.0.0:*               LISTEN      483/postgres    # pg17?
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      482/postgres
root@ubuntu-mstdn:~# ps 483
   PID TTY      STAT   TIME COMMAND
   483 ?        Ss     0:00 /usr/lib/postgresql/17/bin/postgres ...

こいつだ。

対応

ubuntu-mstdn の postgres をさっさと 17 に移行して 5433/tcp を空ければいいんだろう。
けれど、1年後には同じことが起こるのが目に見えているので ubuntu-misky の postgres のポートを地政学的に安全そうな範囲に移すことにした。

postgresql

ポート変更 5433 → 5442

root@ubuntu-misky:~# vim /etc/postgresql/16/main/postgresql.conf
## 64行目
- port = 5433
+ port = 5442

サービス起動

root@ubuntu-misky:~# systemctl daemon-reload
root@ubuntu-misky:~# systemctl start postgresql.service

稼動確認

root@ubuntu-misky:~# systemctl -a | grep postg
postgresql.service           loaded    active   exited    PostgreSQL RDBMS
postgresql@16-main.service   loaded    active   running   PostgreSQL Cluster 16-main    # ✅
system-postgresql.slice      loaded    active   active    system-postgresql.slice
root@ubuntu-misky:~# netstat -pantu
tcp        0      0 127.0.0.1:5442          0.0.0.0:*               LISTEN      485/postgres    # ✅
tcp        0      0 127.0.0.1:5433          0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -

misskey

db ポート変更 5433 → 5442

$ sudo -u misskey -i
misskey@ubuntu-misky:~$ vim live/.config/default.yml
db:
  host: localhost
+ port: 5433
- port: 5442

仮想環境むずかしい。


*1 TCP/IP スタックがインスタンス横断で共用 と思っておけばよいのだろうか。僕はこれをほぼ毎回わすれてる。

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS