PostgreSQL 設定允許訪問IP的操作
阿新 • • 發佈:2021-01-04
PostgreSQL安裝後預設只能localhost:5432訪問
檢驗方法:
curl localhost:5432 # 訪問成功提示 curl: (52) Empty reply from server
curl 127.0.0.1:5432 # 訪問不成功提示 curl: (7) Failed to connect to 172.17.201.227 port 5432: Connection refused
修改pg_hba.conf
pg_hba.conf和postgresql.conf的存放目錄都在(9.5版本)/etc/postgresql/9.5/main
host all all 192.168.1.0/24 trust
表示允許網段192.168.1.0上的所有主機使用所有合法的資料庫使用者名稱訪問資料庫,
其中,數字24是子網掩碼,表示允許192.168.1.0–192.168.1.255的計算機訪問
修改postgresql.conf
修改listen_addresses='localhost',並放開註釋(預設監聽localhost)
# 192.168.1.111 為postgresql本機內網地址 listen_addresses='192.168.1.111'
重啟postgresql
sudo /etc/init.d/postgresql restart
在本機
curl 192.168.1.111:5432 # 訪問成功提示 curl: (52) Empty reply from server
在內網其他機器
curl 192.168.1.111:5432 # 訪問成功提示 curl: (52) Empty reply from server
其他 建立使用者
進入psql控制檯
$ sudo -u postgres -i $ psql
建立使用者 密碼
postgres=# CREATE USER myusername WITH PASSWORD 'mypassword' CREATEDB;
建立資料庫 使用者授權
postgres=# CREATE DATABASE mydb; postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb to myusername; postgres=# \q
測試
$ psql -d mydb; mydb=# \dt
補充:PostgreSQL資料庫開啟IP訪問功能
在PG的安裝目錄的data子資料夾下。
1.postgresql.conf
檢查下面的值是否是監聽所有ip地址的連線請求,如下:
listen_addresses = '*'
如果是則不需要修改。
2.pg_hda.conf
在末尾的地方新增一行,如下:
host all all 0.0.0.0/0 md5
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。