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?

WSL のUbuntu にあるPostgresにアクセスする

Posted at

初めに

ここで行うことは下記のとおりです。

  1. UbuntuにPostgresのインストール
  2. UbuntuのIPアドレス確認
  3. ホストOS(Windows OS自体)から接続するため設定
  4. WindowsにFirewall設定追加

UbuntuにPostgresのインストール

UbuntuにPostgresをインストールしましょう。

パッケージ情報を最新の状態に保つためのコマンド実行する。

$ sudo apt update

パッケージのアップデートを行うコマンド実行(時間が掛かるのでどちらでもいい)

$ sudo apt upgrade

PostgreSQL本体、PostgreSQL周辺の拡張モジュールや便利ツールをインストール

$ sudo apt install postgresql postgresql-contrib

参考)
PostgresSQLを任意のバージョンでインストールする場合、必要なバージョンを探して入れてください。

$ sudo apt-cache search postgresql | grep ^postgresql

UbuntuのIPアドレス確認

下記の方法で確認できます。

Ubuntuから確認

hostname

$ hostname -I
172.27.73.187

ip addr

$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 10.255.255.254/32 brd 10.255.255.254 scope global lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:61:c4:f5 brd ff:ff:ff:ff:ff:ff
    inet 172.27.73.187/20 brd 172.27.79.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe61:c4f5/64 scope link
       valid_lft forever preferred_lft forever

eth0の値がIPです。

Windowsから確認

hostname

PS > wsl -e hostname -I
172.27.73.187

Ubuntu内のPostgres設定ファイル修正

2つのファイルを修正します。

  1. postgresql.conf
  2. pg_hba.conf

エディターは好きなものを使用してください。

$ vi
$ nano

viで修正します。

postgresql.conf

バージョンは一つしか入れないと思うので入れたバージョンで開けるよう、下記コマンドでエディタを開きます。

sudo vi /etc/postgresql/*/main/pg_hba.conf

localhostを外部からも接続できるよう「*」に変更する。

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
listen_addresses = '*'                  # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
 

viのコマンドは下記でできる。

# コマンドモードでlocalhost検索
/localhost (+ enter)
# 文字列置換で入力モードに入りlocalhostを変換しコマンドモードに戻る
cw * (+ esc)
# 書き込みして終わり
:wq

pg_hba.conf

md5のハッシュ値による通信で全てから受けられるように1行追加します。

host    all             all             0.0.0.0/0               md5

viのコマンドは下記でできる。

# 最後の行へ行く
(shift +) g
#挿入モード
i
# 上の1行を貼り付け
# 書き込みして終わり
:wq

WindowsのFirewallに設定追加

WindowsのFirewallの受信の規則(インバウンド)にPostgresnoポートを追加します。

管理者で行ってください。

$ New-NetFirewallRule -DisplayName "PostgreSQL WSL 5432" -Direction Inbound -Protocol TCP -LocalPort 5432 -Action Allow

接続アドレスが決まっている場合は直接指定してください。

New-NetFirewallRule -DisplayName "Allow PostgreSQL from WSL" -Direction Inbound -Protocol TCP -LocalPort 5432 -RemoteAddress <IPアドレス> -Action Allow
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?