0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[DevContainer][PostgreSQL]コンテナ再起動したらDB繋げなくなったんだが?(解決済み

Posted at

さぁて、今日もDevContainerで遊ぶ(仕事する)かぁ

コレとかコレで作ったDevContainerのDockerコンテナーを起動して作業しようとしら。。。

クソがっ!!
# psql -U postgres
psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?

なんなんお前?1回だけしか使えんのか?

えっ、DevContainerって初回起動のときだけしかDBって使えないの?
マジでクソみたいなツールやんけ!!

って思ってた時期が俺にもありました。

原因

ポスグレのサービスが死んでた(おいおいおい、アイツ死んだわ。。。

コンテナを再起動した際にDockerDesktopで見たLogsに以下のような内容が記述されていた。

logs
Container started
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".

Data page checksums are disabled.

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

何なんコイツ、毎回initdbでDB作ろうとしてるんか?
/var/lib/postgresql/data は ボリュームマウントされているみたいなので、Dockerを止めても値が残るのは別にいいんだけど、それが有るおかげでinitdbでコケてサービスが立ち上がらないのがマジで納得いかない。。。
→コレが原因か分からんけど、とにかくサービスが起動していないのは間違いない

解決策

とりあえず、ポスグレのサービスが立ち上がっているかの確認コマンドをぶっ叩く

service --status-all

結果としては以下のような感じになる

vscode ➜ /workspaces/src $ service --status-all
 [ - ]  apache-htcacheclean
 [ + ]  apache2
 [ - ]  cron
 [ - ]  exim4
 [ ? ]  hwclock.sh
 [ - ]  postgresql
 [ - ]  procps
 [ - ]  rsync
 [ - ]  sudo
 [ - ]  sysstat

結果がハイフンになっている場合

サービスが死んでるという認識でいいと思われる。

以下コマンドでサービスを立ち上げる
service postgresql start

結果

コレでポスグレにアクセスが出来るようになったので、
とりあえずコレが解決策としてひとまずメモとして残す。

まとめ

たぶんdevcontainer.jsonとかにちゃんと設定をすれば大丈夫だとは思うんだけど、現在そこまで調べる余裕がないので一旦コレでおしまい!!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?