U-Yuri’s 健忘録

U-Yuri’s 備忘録

プログラミングを勉強しています。アウトプットに活用しているブログです。

外部からPostgreSQLに接続する方法

postgresql.confを編集

% vim /usr/local/var/postgresql@14/postgresql.conf

■60行目付近のlisten_addresses = 'localhost'を編集

listen_addresses = '*'  #'*'へ変更

 

■pg_hba.confを編集

※ ファイルは下記にあり(findコマンドで検索してください)

% vim /usr/local/var/postgresql@14/pg_hba.conf

・90行目付近に認証を受け付けるIPの範囲を追記する。 ※ trustは全てmd5に変更。

IPv4 local connections:
host    all             all             127.0.0.1/32            md5   #元々ある行
host    all             all             192.168.1.1/16          md5  #追記行

・終わったらrestart

% brew services restart postgresql

・外部サーバーからアクセスしてみる ※ 作ったDB確認・ユーザ名は以下で確認できる


postgres=# \l
 List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |  Access privileges  
-----------+----------+----------+---------+-------+---------------------
 hogehoge  | postgres | UTF8     | C       | C     | 
 hogehoge2 | postgres | UTF8     | C       | C     | 
 hogehoge3 | postgres | UTF8     | C       | C     | 
 postgres  | macuser  | UTF8     | C       | C     | 
 template0 | macuser  | UTF8     | C       | C     | =c/macuser         +
           |          |          |         |       | macuser=CTc/macuser
 template1 | macuser  | UTF8     | C       | C     | =c/macuser         +
           |          |          |         |       | macuser=CTc/macuser
(6 rows)

・上記から接続したいサーバを選び、アクセス。


@debian:~$ psql -U postgres -h 192.168.10.103  -p 5432 -d hogehoge

・以下になればOK


hogehoge=#