postgresql 在 Ubuntu 中打开端口

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

Open Port in Ubuntu

postgresqlubuntuport

提问by Aram Papazian

So I'm using AWS using EC2 and I'm trying to open up a port for Postgresql. In AWS I already have it open:

所以我正在使用 EC2 使用 AWS,并且我正在尝试为 Postgresql 打开一个端口。在 AWS 中,我已经打开了它:

TCP
Port (Service)      Source                  Action
0 - 65535           sg-92aadda2 (default)   Delete
22 (SSH)            0.0.0.0/0               Delete
80 (HTTP)           0.0.0.0/0               Delete
5432                0.0.0.0/0               Delete

When I do netstat it looks as though the port is listening:

当我执行 netstat 时,它看起来好像端口正在侦听:

# netstat -an | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN

When I do a localhost nmap I get the following:

当我执行 localhost nmap 时,我得到以下信息:

 Nmap scan report for localhost (127.0.0.1)
 Host is up (0.000010s latency).
 Not shown: 997 closed ports
 PORT      STATE SERVICE
 22/tcp    open  ssh
 80/tcp    open  http
 5432/tcp  open  postgresql

And here's where the fun begins. When I do an nmap from an alternative host I get the following:

这就是乐趣开始的地方。当我从替代主机执行 nmap 时,我得到以下信息:

PORT      STATE  SERVICE
22/tcp    open   ssh
80/tcp    open   http
5432/tcp  closed postgresql

I also looked at my iptables to see if I was missing something, but the iptables look empty (which should mean they aren't really doing much)

我还查看了我的 iptables,看看我是否遗漏了什么,但是 iptables 看起来是空的(这应该意味着它们并没有真正做很多事情)

$ iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
REJECT     all  --  anywhere             127.0.0.0/8          reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:postgresql
ACCEPT     icmp --  anywhere             anywhere
LOG        all  --  anywhere             anywhere             limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
DROP       all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere

Am I missing something cause I can't seem to figure out how to access the ip. Whenever I try I get the following error:

我是否遗漏了什么,因为我似乎无法弄清楚如何访问 ip。每当我尝试时,都会收到以下错误:

Is the server running on host "xx.xx.xx.xx" and accepting TCP/IP connections on port 5432?

How do I make it so that I can open up the port so that external servers have access to it? Thanks in advance =) Lemme know if you need any additional data.

我如何才能打开端口以便外部服务器可以访问它?提前致谢 =) 让我知道您是否需要任何其他数据。

EDIT: As asked below, I tested telnetting, and I was able to telnet into the localhost, but when attempting from the outside I get:

编辑:如下所述,我测试了 telnet 连接,并且能够 telnet 到本地主机,但是当从外部尝试时,我得到:

$ telnet xx.xx.xx.xx 5432
Trying xx.xx.xx.xx...
telnet: Unable to connect to remote host: Connection refused

Also, I double checked and I was properly able to telnet into ssh:

另外,我仔细检查了一下,我可以正确地 telnet 到 ssh:

$ telnet xx.xx.xx.xx 22
Trying xx.xx.xx.xx...
Connected to xx.xx.xx.xx.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1

回答by TeTeT

Edit /etc/postgresql/<version>/main/postgresql.confand set the listen_addressesto your outgoing interface or all. Restart postgresql: sudo service postgresql restart.

编辑/etc/postgresql/<version>/main/postgresql.conf并设置listen_addresses为您的传出接口或全部。重启 postgresql: sudo service postgresql restart.

回答by angelous

It works for me the last method (thks Julio):

最后一种方法对我有用(感谢 Julio):

Edit:postgresql.conf

编辑:postgresql.conf

sudo nano /etc/postgresql/9.3/main/postgresql.conf

sudo nano /etc/postgresql/9.3/main/postgresql.conf

Enable or add:

启用或添加:

listen_addresses = '*'

listen_addresses = '*'

Restart the database engine:

重启数据库引擎:

sudo service postgresql restart

sudo service postgresql restart



Besides, you can check the file: pg_hba.conf

此外,您可以查看文件:pg_hba.conf

sudo nano /etc/postgresql/9.3/main/pg_hba.conf

sudo nano /etc/postgresql/9.3/main/pg_hba.conf

And add your network or host address:

并添加您的网络或主机地址:

host all all 192.168.1.0/24 md5

host all all 192.168.1.0/24 md5

回答by kamasteve

If you have edited postgresql.confand main/pg_hba.confand still having a problem, please try

如果你已经编辑了postgresql.confmain/pg_hba.conf并且仍然有问题,请尝试

sudo ufw allow 5432/tcp

to unblock psql port

解锁 psql 端口