postgres遠程連接方式配置
配置pg_hba.conf文件 目錄C:\Program Files\PostgreSQL\9.5\data
(QXY)主機
[postgres@qxy data]$ pwd
/spark/pgsql/data
[postgres@qxy data]$ cat pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# “l(fā)ocal” is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
在# IPv4 local connections:
下面添加一行,內(nèi)容為 “host all all 192.168.40.1/24 md5”,代表192.168.40網(wǎng)段的IP地址的所有用戶都可以連接,/24代表網(wǎng)段,如果是/32 需要寫完整的IP地址
添加之后的內(nèi)容如下:
# “l(fā)ocal” is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.40.1/24 md5
2. 修改postgres監(jiān)聽的IP地址,默認是localhost ---------- 有時候默認就是OK的,可以看看用不用修改
(QXY)主機
postgres@qxy data]$ cat postgresql.conf
# - Connection Settings -
#listen_addresses = ‘localhost'
# what IP address(es) to listen on;
listen_addresses = ‘*'
# what IP address(es) to listen on; =======新增這行,把localhost改成*,監(jiān)聽所有的IP
# comma-separated list of addresses;
# defaults to ‘localhost'; use ‘*' for all
# (change requires restart)
3.重新啟動postgres 服務
在service服務list中重啟: postgresql-x64-9.5
4. 大功告成,遠程測試下連接postgresDB吧
補充:PostgreSql允許postgres用戶在一個特定的IP進行遠程登錄,并具有所有庫任何操作權(quán)限
1.pgsql允許遠程訪問:
安裝PostgreSQL數(shù)據(jù)庫之后,默認是只接受本地訪問連接。如果想在其他主機上訪問PostgreSQL數(shù)據(jù)庫服務器,就需要進行相應的配置。
a.如果是windows安裝的postgresql,配置遠 程連接PostgreSQL數(shù)據(jù)庫的步驟很簡單,只需要修改安裝目錄data文件夾下的pg_hba.conf和postgresql.conf。

b.如果是linux上安裝的postgresql,同樣是修改是這兩個文件:
文件位置:
cd /etc/postgresql/9.3/main/

2.修改pg_hba.conf文件,配置用戶的訪問權(quán)限(#開頭的行是注釋內(nèi)容):
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 222.73.203.68/24 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
其中,第10條是新添加的內(nèi)容,表示允許網(wǎng)段 222.73.203.68上的這個特定主機使用所有合法的數(shù)據(jù)庫用戶名訪問數(shù)據(jù)庫。
如果允許所有主機訪問,不需要特別嚴格的權(quán)限控制時,可以直接將第10行,IPv4中換成:
host all all 0.0.0.0/0 trust 即可
3.修改postgresql.conf文件,將數(shù)據(jù)庫服務器的監(jiān)聽模式修改為監(jiān)聽所有主機發(fā)出的連接請求:
定位到#listen_addresses='localhost'。PostgreSQL安裝完成后,默認是只接受來在本機localhost的連接請 求。
將行開頭都#去掉,將行內(nèi)容修改為listen_addresses='*'來允許數(shù)據(jù)庫服務器監(jiān)聽來自任何主機的連接請求
4.查看postgresql默認端口號:(一般默認端口號是:5432)

5.查看防火強狀態(tài):

如果防火墻active是開啟狀態(tài),允許5432端口入站:

查看防火墻狀態(tài),查看5432端口是否增加成功:

6.回到 222.73.203.68這臺機器上的客戶端就可以對所要遠程機器上的數(shù)據(jù)庫進行遠程訪問了

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- 解決postgresql無法遠程訪問的情況
- Postgresql開啟遠程訪問的步驟全紀錄
- Postgresql設置遠程訪問的方法(需要設置防火墻或者關(guān)閉防火墻)