从局域网访问 PostgreSQL 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22080307/
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-11 00:38:05  来源:igfitidea点击:

Access PostgreSQL server from LAN

postgresqlpostgresql-9.3

提问by Simón Oro?o

I've been trying to edit pg_hba.conf file in order to be able to access the server using just the IP address with, so far, no success.

我一直在尝试编辑 pg_hba.conf 文件,以便能够仅使用 IP 地址访问服务器,到目前为止,没有成功。

For example, I can access using ?localhost?, but I want to access using the IP address that my router gave me wich is something like 192.168.1.X

例如,我可以使用 ?localhost? 访问,但我想使用路由器给我的 IP 地址进行访问,例如 192.168.1.X

This is mi pg_hba.conf:

这是 mi pg_hba.conf:

# 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
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                trust
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust
host all all 0.0.0.0/0 trust

Any help?

有什么帮助吗?

回答by harmic

First, edit the postgresql.conf file, and set listen_addresses. The default value of 'localhost' will only listen on the loopback adaptor. You can change it to '*', meaning listen on all addresses, or specifically list the IP address of the interfaces you want it to accept connections from. Note that this is the IP address which the interface has allocated to it, which you can see using ifconfigor ip addrcommands.

首先,编辑 postgresql.conf 文件,并设置listen_addresses。'localhost' 的默认值将只侦听环回适配器。您可以将其更改为“*”,这意味着侦听所有地址,或者专门列出您希望它接受连接的接口的 IP 地址。请注意,这是接口分配给它的 IP 地址,您可以使用ifconfigip addr命令查看。

You must restart postgresql for the changes to listen_addresses to take effect.

您必须重新启动 postgresql 以使对 listen_addresses 的更改生效。

Next, in pg_hba.conf, you will need an entry like this:

接下来,在pg_hba.conf 中,您将需要这样的条目:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    {dbname}        {user}          192.168.1.0/24          md5

{dbname} is the database name you are allowing access to. You can put "all" for all databases.

{dbname} 是您允许访问的数据库名称。您可以为所有数据库放置“全部”。

{user} is the user who is allowed to connect. Note that this is the postgresql user, not necessarily the unix user.

{user} 是被允许连接的用户。请注意,这是 postgresql 用户,不一定是 unix 用户。

The ADDRESS part is the network address and mask that you want to allow. The mask I specified will work for 192.168.1.x as you requested.

ADDRESS 部分是您要允许的网络地址和掩码。我指定的掩码将按照您的要求适用于 192.168.1.x。

The METHOD part is the authentication method to use. There are a number of options there. md5 means it will use an md5 hashed password. 'trust' which you had in your sample means no authentication at all - this is definitely not recommended.

METHOD 部分是要使用的身份验证方法。那里有很多选择。md5 意味着它将使用 md5 散列密码。您在样本中拥有的“信任”意味着根本没有身份验证 - 这绝对是不推荐的。

Changes to pg_hba.conf will take effect after reloading the server. You can to this using pg_ctl reload(or via the init scripts, depending on your OS distro).

对 pg_hba.conf 的更改将在重新加载服务器后生效。您可以使用pg_ctl reload(或通过 init 脚本,具体取决于您的操作系统发行版)。