Ubuntu24.04でのPostgreSQL設定
■インストール
コマンド:sudo apt install postgresql postgresql-contrib
↑インストール時間は長くありません。比較的短時間でプロンプトになります。
■postgres ユーザになって psql を起動して動作確認
コマンドその1:sudo -u postgres psql
:~$ sudo -u postgres psql
psql (16.4 (Ubuntu 16.4-0ubuntu0.24.04.2))
Type "help" for help.
↑psql16.4とあります。
コマンドその2:select version();
version
----------------------------------------------------------------------------------
PostgreSQL 16.4 中略 (Ubuntu 13.2.0-23ubuntu4) 13.2.0, 64-bit
(1 row)
・・・
(END)
↑たしかにPostgreSQL16.4だけがインストールされていました。
■外部接続許可
1;postgresql.confを編集
パス:/etc/postgresql/16/main/postgresql.conf
コマンド:sudo gnome-text-editor /etc/postgresql/16/main/postgresql.conf
listen_addressesをオールにする。(カッコ内を半角英数アスタリスクにする)
# - Connection Settings -
listen_addresses = '*'
2;pg_hba.confを編集(例)
パス:/etc/postgresql/16/main/pg_hba.conf
コマンド:sudo gnome-text-editor /etc/postgresql/16/main/pg_hba.conf
host all all 192.168.0.0/24 trust
host all all 192.168.1.0/24 md5
外部ネット接続のない環境などセキュリティの問題ない接続環境ならば
trust(ノーガード、つまりなんでも受入可能)でもよい。
ということで、実際の設定は↓こんな感じ・・・
# Database administrative login by Unix domain socket
#local all postgres peer
local all postgres md5
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
#local all all peer
local all all trust
# IPv4 local connections:
#host all all 127.0.0.1/32 scram-sha-256
host all all 127.0.0.1/32 trust
host all all 192.168.0.0/24 trust←これダメ(後述)
host all all 192.168.1.0/24 trust
■PostgreSQLユーザ設定
出典
https://lets.postgresql.jp/documents/tutorial/ubuntu/2
1:初期設定されている管理ユーザpostgresにパスワードを設定
■PostgreSQL起動停止操作確認
1;PosgreSQLサーバを起動
・起動コマンド sudo service postgresql start
2:PosgreSQLサーバを停止
・停止コマンド sudo service postgresql stop
3:postgreSQL動作状態確認コマンド:pg_isready
~$ pg_isready
/var/run/postgresql:5432 - accepting connections
↑(起動後)接続可能です、という結果
/var/run/postgresql:5432 - no response
↑(停止後)反応がありません、という結果
■接続確認
サーバ上のpgAdmin4からは接続できました。
■外部PCからの接続確認
1:Ubuntu24.04(PosgreSQLサーバ)のIPアドレスをメモ
2:外部PC上のpgAdmin4でサーバ接続設定(IP、ID、PW)
3:外部PCからPosgreSQLサーバへ接続・・・頓挫中
↑↑ここで頓挫中241012
外部PCのpgAdmin4にて「接続が許可されない云々」とな。
pg_hba.confのどこかを設定ミスしているようです。
→わかりました!!!
(ダメ)host all all 192.168.0.0/24 trust
↑これで全セグメントから接続できる、と思い込んでました。
24ではだめですね。
(OK)host all all 192.168.1.0/24 trust
host all all 192.168.2.0/24 trust
↑といったふうにちゃんとセグメントごとに定義
これでうまく外部PCから接続できました。
4:テーブル操作などで動作確認・・・後日、別途アップ予定
5:接続解除(データベース切断)
以上、とりあえず終了。