LoginSignup
3
2

More than 1 year has passed since last update.

WSLのLinuxからWindows10側のPostreSQL(などのサーバー)を呼び出す

Last updated at Posted at 2021-06-30

経緯

  • WSLのLinxからWindows10側のPostgreSQL(などのサーバー)を呼び出そうとして、すぐにできなかったのでメモ
  • WSLのLinuxとWindows 10間の通信で考慮することがある
  • 結構、大変なので、WSL側にPostgreSQL Serverを入れるなどを検討した方がよいのかもしれない

環境

Windows 10 側

  • Windows 10
  • postgresql-13.3-2-windows-x64.exe
  • Windows Terminal
  • WSL2

WSL Ubuntu 側

  • Windows Subsystem for Linux Ubuntu 20.04
  • postgresql-client-common
  • postgresql-client-12
  • net-tools
sudo apt install postgresql-client-common
sudo apt install postgresql-client-12
sudo apt install net-tools

WSLのLinux上でのIPアドレスを確認(WSLの操作)

ifconfig

WSLからWindows 10側にアクセスするためのIPアドレスを取得する(WSLの操作)

  • nameserverがWindows10側のIPになるのを利用している? (*1を参考にした)
grep nameserver /etc/resolv.conf | awk '{print $2}'
  • 起動の度にIPが変わるようなので、Windows10側のIPを書きだすコマンドを .bashrc に追加するなどの仕組みが必要そう (*1 ではhostsに追記しているようだ)
  • 今回は試していないが、以下が *1 にあるの内容
cat /etc/hosts | grep 172.; test $? -eq 0 && $1 || echo -e "$(grep nameserver /etc/resolv.conf | awk '{print $2, " host"}')\n$(cat /etc/hosts)" | sudo tee /etc/hosts

PostgreSQL Client Authentication Configuration File(アクセス制御)の設定 (Windows 10側の設定)

  • PostgreSQL の pg_hba.conf を編集

場所

C:\Program Files\PostgreSQL\13\data\

編集内容例

  • WSLから通信できるように設定する
  • (クラスBのプライベートIPアドレスを指定したつもりだが正しいだろうか)
host    all             all             172.16.0.0/12           trust

アンチウィルスなどのセキュリティアプリの設定

  • 各アプリで、通信できるように設定を見直す

Windows Defender Firewallの設定例

  • 「受信の規則」で「プロトコルおよびポート」を開放
  • 「スコープ」を指定して、IPアドレスを指定

defender.png

rule.png

port.png

range.png

psqlの実行

psql -h 172.x.x.x -U postgres

参考資料

3
2
1

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
3
2