同じネットワークにあるPCのpostgresqlに接続できません。
どういった対応が必要か回答お願いします。
ホスト側(postgresqlインストールPC)
192.168.0.100
C:\Users\USER>psql -V psql (PostgreSQL) 14.1
クライアント側
192.168.0.10
実行コード(ホスト側PCで動かしたら接続できましたが、クライアント側ではエラー。ただし、ホスト側で動かしたときは host = "localhost" にしてました。)
Python
1from datetime import datetime 2 3import psycopg2 4 5table = "testtb" 6 7def cnct(): 8 try: 9 con = psycopg2.connect( 10 host = "192.168.0.100", 11 port = 5432, 12 user = "postgres", 13 password = "root", 14 dbname = "postgres", 15 connect_timeout = 10 16 ) 17 cur = con.cursor() 18 print("<< cnct current >>") 19 return con, cur 20 except Exception as e: 21 print("<< cnct error >>") 22 print("type(e): {}".format(type(e))) 23 print("e: {}".format(e)) 24 return None 25 26 27def discnct(con, cur): 28 try: 29 cur.close() 30 con.close() 31 print("<< discnct current >>") 32 except Exception as e: 33 print("<< discnct error >>") 34 print("type(e): {}".format(type(e))) 35 print("e: {}".format(e)) 36 37 38def select(sql): 39 try: 40 cur.execute(sql) 41 results = cur.fetchall() 42 print("<< select current >>") 43 return results 44 except Exception as e: 45 print("<< select error >>") 46 print("type(e): {}".format(type(e))) 47 print("e: {}".format(e)) 48 return None 49 50 51if __name__ == "__main__": 52 # cnct 53 cnct_info = cnct() 54 if cnct_info: 55 con, cur = cnct_info 56 57 # select 58 results = select("SELECT * FROM testtb;") 59 if results: 60 for i in results: 61 print(i) 62 63 # discnct 64 discnct(con, cur)
確認したこと
下記を参考に、ホスト側のPostgreSQLの設定ファイルを確認したが、
pg_hba.confのmethodがmd5ではなくscram-sha-256となっていること以外は、
サイトの内容と同じだった。
【PostgreSQL】他のPCから接続する設定を解説します(Windows)
試したこと
下記を参考に、WindowsDefenderファイアーウォールの受信規則追加。
PostgreSQL へ他の端末から接続するための設定
エラー
WindowsDefenderファイアーウォールの受信規則追加前の結果
<< cnct error >> type(e): <class 'psycopg2.OperationalError'> e: connection to server at "192.168.0.100", port 5432 failed: timeout expired
WindowsDefenderファイアーウォールの受信規則追加した際の結果
<< cnct error >> type(e): <class 'psycopg2.OperationalError'> e: connection to server at "192.168.0.100", port 5432 failed: FATAL: no pg_hba.conf entry for host "192.168.0.10", user "postgres", database "postgres", no encryption
追記
pg_hba.confに下記最下行を追記したらクライアント側から接続できました。
しかし、この方法だと受け入れるPCを都度入れる必要がありそうですが、すべてのPCから受け入れるようにするにはどうすればよいでしょうか。
アドレスの部分に"*"とか入れてみましたが、could not load pg_hba.confなどとエラーが返されてしまいました。
pg_hba.conf
1# IPv4 local connections: 2host all all 127.0.0.1/32 scram-sha-256 3host all all 192.168.0.10/32 trust
(192.168.0.10/32と記述して接続できましたが、ホスト側のコマンドプロンプトでサブネットマスク確認したら 255.255.255.0 だったので、CIDR表記として正しいのは 192.168.0.10/24 でした。192.168.0.10/24を記述しても接続できたので、192.168.0.10/24と記述することにします。)


回答1件
あなたの回答
tips
プレビュー