将 PostgreSQL 配置为仅适用于 LOCALHOST 或指定的 ip + 端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11753296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:37:50  来源:igfitidea点击:

Configure PostgreSQL to work for only LOCALHOST or specified ip + port

postgresqlconfigurationipconfig

提问by Ankur Loriya

I want to configure PostgreSQL to accept connections only from a specified IP. It should not accept requests from any other IP.

我想将 PostgreSQL 配置为仅接受来自指定 IP 的连接。它不应接受来自任何其他 IP 的请求。

回答by francs

The following pg_hba.conf allows local and specified Ip have privilege login, but reject others。

下面的 pg_hba.conf 允许本地和指定的 Ip 有权限登录,但拒绝其他人。

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             all                                     trust
host    testdb          testuser      192.168.1.1/32             md5
host    all             all           0.0.0.0/0                 reject 

回答by A.H.

The most easiest way is to make PostgreSQL listening only on localhostfor incoming connections. The relevant parameter is listen_addressesin postgresql.conf. The docu is here.

最简单的方法是让 PostgreSQL 只监听localhost传入的连接。相关参数listen_addressespostgresql.conf. 文档在这里

回答by Jacob George

Check the pg_hba.conffile in the data folder of PostgreSQL. This is the client authentication configuration file.

检查PostgreSQL 数据文件夹中的pg_hba.conf文件。这是客户端身份验证配置文件。

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    testdb           testuser      192.168.1.1               md5
local   testdb           all                                     md5

Add the above to the pg_hba.conf file

将以上内容添加到 pg_hba.conf 文件中