| PostgreSQL | Podman(Docker)でPostgreSQLを使う | |
| (not ever) |
| Podman(Docker)でPostgreSQLを使う |
●公式イメージのpullと初回の起動
使用したOSはRHEL 9.2です。次のコマンドで公式PostgreSQLイメージのpullと初回のコンテナ起動を行います。
podman run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=SecretPassword postgres
podman exec -it postgres /bin/bash
# psql -U postgres
postgres=# (以下を入力)
create table scores (name varchar not null primary key, score integer not null);
insert into scores(name,score) values('Taro', 80);
insert into scores(name,score) values('Jiro', 75);
select * from scores;
exit
# exit
●ホストOSからコンテナのPostgreSQLに接続する
ホストOS側では、PostgreSQLイメージをpullしたからといってpsqlクライアントが同時に入るわけではないので、
別途インストールが必要です。余談ですがLinux系OSではどれでもクライアントのパッケージ名がpostgresql、
サーバのパッケージ名がpostgresql-serverとなっていると思います。
sudo dnf install -y postgresql
PGPASSWORD=SecretPassword psql -h localhost -p 5432 -U postgres postgres=# (以下を入力) select * from scores; exit;
●コンテナ内の設定を見てみる
podman exec -it postgres /bin/bash # env | grep PG ------ PG_MAJOR=15 PG_VERSION=15.4-1.pgdg120+1 PGDATA=/var/lib/postgresql/data ------ # cd $PGDATA # more postgresql.conf # more pg_hba.conf
local all all trust host all all 127.0.0.1/32 trust (中略) host all all all scram-sha-256
ちなみに pg_hba.conf を編集したい場合、デフォルトではコンテナの中にはvimもnanoもテキストエディタらしきものは入っていないので、 自分でインストールする必要があります。コンテナのOSはDebianです。 コンテナの中に入って /etc/debian-version や /etc/issue を見ると分かります。 したがって、
# apt-get install nano vim
●ホストOSからのみPW不要で接続可能にする
Podmanでルートレスコンテナを起動した場合、コンテナ内から外(ホストOS側)を見るIPアドレスは
「100.0.2.100」になります。これはコンテナ内の/etc/hostsを見ると分かります。
これを使って、ホストOSからのみPW不要で接続可能にすることも可能です。開発時などには便利かもしれません。
pg_pba.conf を編集して、
host all all 10.0.2.100/32 trust
●再起動と停止
PostgreSQLプロセスの停止や再起動ってどうやるの?
コンテナの中に入って、標準のpg_ctlコマンドで試してみます。
# pg_ctl restart pg_ctl: cannot be run as root Please log in (using, e.g., "su") as the (unprivileged) user that will
# su postgres $ pg_ctl restart waiting for server to shut down....
podman restart postgres
| もくじ | ||
| PostgreSQL | (C) 2002-2023 MISUMI URANO (2024/01/04 - (not ever)) |