S. *_*. N 4 postgresql google-compute-engine
我已经在谷歌云上安装了一个 PostgreSQL,我希望能够使用 pg-admin 从我的 PC 远程访问它。
当我尝试连接到我的实例时,我确实收到以下错误:
无法连接到服务器:
无法连接到服务器:连接被拒绝 (0x0000274D/10061) 服务器是否在主机“[我的 VM 实例的外部 IP]”上运行并接受端口 5432 上的 TCP/IP 连接?
我认为这可能是因为谷歌云防火墙阻止了我(也许!),我为我的实例指定了以下防火墙,我使用我的 IP 地址来定义源过滤器:

我是否错过了一些东西,因为我仍然无法访问它,有人可以帮助我吗?我已经不知道了,我已经检查了很多教程,并且我做了很多我应该解决的事情。有人知道吗?
我按照1上的教程进行操作,我可以从一台机器远程连接到另一台机器。我可以通过错过配置防火墙规则来重现您的情况。您已使用标签设置防火墙规则。该标签也需要在 VM 上。如果您使用标签创建防火墙规则,则该标签需要打开(在我的情况下,标签的标签名称是 postgre-5432)网络标签:
您可以在 VM 实例部分编辑您的 Compute Engine 实例。
在这种情况下,标签用于能够在同一网络中为特定机器设置防火墙规则。
这是我的入口流量防火墙规则的定义。这是我的运行 postgresql 服务器的计算引擎实例在网络标签部分配置的标签:
在源 Ip 范围中,您需要将要连接的计算机的 PUBLIC IP 地址放置到运行 postgresql 服务器的计算机,因为这是入口防火墙规则。使用该 ip 作为源 ip,您不应该有任何标签问题。我鼓励您使用标签,因为一切都会更安全。
您可以在此地址中检查本地连接的 PUBLIC IP:http : //ip4.me/
然后在您提到的配置文件上,您需要放置连接的公共 IP。此 IP 需要与您在入口防火墙规则中使用的 IP 相同。
sudo nano /etc/postgresql/9.5/main/pg_hba.conf: host all all PUBLIC_IP/32 md5
在这里您需要为listen_addresses 设置*,因为* 表示任何地址。无论如何,您正在控制能够在 pg_hba.conf 中连接到您的实例的 ip:
sudo nano /etc/postgresql/9.5/main/postgresql.conf listen_addresses = '*' # 要监听的 IP 地址;
管理员:
在这里你可以看到如何在 ubuntu 2上安装 pgAdmin4 在 这里你可以看到如何从命令行连接到 postgres。[3
为了能够从 pgAdmin4 连接到您的 postgres 远程实例,您需要将主机名/地址设置为运行 postgresql 服务器的 Compute Engine 实例 Ip。如果您遵循此处的教程1,您也应该有一个名为 postgres 的默认数据库。
希望这可以帮助!
1 https://cloud.google.com/community/tutorials/setting-up-postgres#creating-a-compute-engine-instance
2 https://askubuntu.com/questions/831262/how-to-install-pgadmin-4-in-desktop-mode-on-ubuntu
3 https://www.postgresql.org/message-id/001f01c018c2 $830133b0$64898cd5%40northlink.gr
PostgreSQL 还必须配置为允许远程连接,否则连接请求将失败,即使所有防火墙规则都正确并且 PostgreSQL 服务器正在侦听正确的端口。
无法创建链接,但这是一个相当长的答案,所以这可能会有所帮助。
nc或netcatnmapnetstatlsofpostgresql.confpg_hba.confnc或netcat$ nc -zv 4.3.2.1 5432
Run Code Online (Sandbox Code Playgroud)
在哪里
-v Produce more verbose output.
-z Only scan for listening daemons, without sending any data to
them. Cannot be used together with -l.
Run Code Online (Sandbox Code Playgroud)
可能的结果:
Connection to 4.3.2.1port [tcp/postgresql] succeeded!
好极了。
nc: connect to 4.3.2.1 port 8000 (tcp) failed: Connection refused
端口被防火墙打开,但服务要么不监听,要么拒绝连接。
防火墙正在阻止。
nmap$ nmap 4.3.2.1
Starting Nmap 7.70 ( https://nmap.org ) at 2019-09-09 18:28 PDT
Nmap scan report for 1.2.3.4.bc.googleusercontent.com (4.3.2.1)
Host is up (0.12s latency).
Not shown: 993 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp closed http
443/tcp closed https
3389/tcp closed ms-wbt-server
4000/tcp closed remoteanything
5432/tcp open postgresql # firewall open, service up and listening
8000/tcp closed http-alt # firewall open, is service up or listening?
Run Code Online (Sandbox Code Playgroud)
netstat$ netstat -tuplen
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:4000 0.0.0.0:* LISTEN 1000 4223185 29432/beam.smp
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1000 4020942 15020/postgres
tcp 0 0 127.0.0.1:5433 0.0.0.0:* LISTEN 1000 3246566 20553/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 1000 4020941 15020/postgres
tcp6 0 0 ::1:5433 :::* LISTEN 1000 3246565 20553/postgres
udp 0 0 224.0.0.251:5353 0.0.0.0:* 1000 4624644 6311/chrome --type=
udp 0 0 224.0.0.251:5353 0.0.0.0:* 1000 4624643 6311/chrome --type=
udp 0 0 224.0.0.251:5353 0.0.0.0:* 1000 4625649 6230/chrome
udp 0 0 0.0.0.0:68 0.0.0.0:* 0 20911 -
udp6 0 0 :::546 :::* 0 4621237 -
Run Code Online (Sandbox Code Playgroud)
在哪里
-t | --tcp
-u | --udp
-p, --program
Show the PID and name of the program to which each socket belongs.
-l, --listening
Show only listening sockets. (These are omitted by default.)
-e, --extend
Display additional information. Use this option twice for maximum
detail.
--numeric, -n
Show numerical addresses instead of trying to determine symbolic host,
port or user names.
Run Code Online (Sandbox Code Playgroud)
当在运行 PostgreSQL 的实例上发出时,您没有看到下面的行,这意味着 PostgreSQL 没有配置为远程连接:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 1001 238400 30826/postgres
tcp6 0 0 :::5432 :::* LISTEN 1001 238401 30826/postgres
Run Code Online (Sandbox Code Playgroud)
lsof检查实例服务是否正在运行。
$ sudo lsof -i -P -n | grep LISTEN
systemd-r 457 systemd-resolve 13u IPv4 14870 0t0 TCP 127.0.0.53:53 (LISTEN)
sshd 733 root 3u IPv4 19233 0t0 TCP *:22 (LISTEN)
sshd 733 root 4u IPv6 19244 0t0 TCP *:22 (LISTEN)
postgres 2733 postgres 3u IPv4 23655 0t0 TCP 127.0.0.1:5432 (LISTEN)
python3 26083 a_user 4u IPv4 392307 0t0 TCP *:8000 (LISTEN)
Run Code Online (Sandbox Code Playgroud)
要从您的一端连接,您将需要您的机器的公共 IP 地址,以及 Google Compute Engine (GCE) 实例的公共 IP 地址。
(来自这篇文章。)
$ dig +short myip.opendns.com @resolver1.opendns.com
4.3.2.1
Run Code Online (Sandbox Code Playgroud)
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
access-news us-east1-d n1-standard-2 10.142.0.5 34.73.156.19 RUNNING
lynx-dev us-east1-d n1-standard-1 10.142.0.2 35.231.66.229 RUNNING
tr2 us-east1-d n1-standard-1 10.142.0.3 35.196.195.199 RUNNING
Run Code Online (Sandbox Code Playgroud)
如果您还需要实例的网络标签:
$ gcloud compute instances list --format='table(name,status,tags.list())'
NAME STATUS TAGS
access-news RUNNING fingerprint=mdTPd8rXoQM=,items=[u'access-news', u'http-server', u'https-server']
lynx-dev RUNNING fingerprint=CpSmrCTD0LE=,items=[u'http-server', u'https-server', u'lynx-dev']
tr2 RUNNING fingerprint=84JxACwWD7U=,items=[u'http-server', u'https-server', u'tr2']
Run Code Online (Sandbox Code Playgroud)
仅处理下面的 GCE 防火墙规则,但请确保iptables不会无意中阻止流量。
也可以看看
$ gcloud compute firewall-rules list
NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED
default-allow-http default INGRESS 1000 tcp:80 False
default-allow-https default INGRESS 1000 tcp:443 False
default-allow-icmp default INGRESS 65534 icmp False
default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False
default-allow-rdp default INGRESS 65534 tcp:3389 False
default-allow-ssh default INGRESS 65534 tcp:22 False
pg-from-tag1-to-tag2 default INGRESS 1000 tcp:5432 False
To show all fields of the firewall, please show in JSON format: --format=json
To show all fields in table format, please see the examples in --help.
Run Code Online (Sandbox Code Playgroud)
包含网络标签的更全面的列表(来自gcloud compute firewall-rules list --help):
$ gcloud compute firewall-rules list --format="table( \
name, \
network, \
direction, \
priority, \
sourceRanges.list():label=SRC_RANGES, \
destinationRanges.list():label=DEST_RANGES, \
allowed[].map().firewall_rule().list():label=ALLOW, \
denied[].map().firewall_rule().list():label=DENY, \
sourceTags.list():label=SRC_TAGS, \
sourceServiceAccounts.list():label=SRC_SVC_ACCT, \
targetTags.list():label=TARGET_TAGS, \
targetServiceAccounts.list():label=TARGET_SVC_ACCT, \
disabled \
)"
NAME NETWORK DIRECTION PRIORITY SRC_RANGES DEST_RANGES ALLOW DENY SRC_TAGS SRC_SVC_ACCT TARGET_TAGS TARGET_SVC_ACCT DISABLED
default-allow-http default INGRESS 1000 0.0.0.0/0 tcp:80 http-server False
default-allow-https default INGRESS 1000 0.0.0.0/0 tcp:443 https-server False
default-allow-icmp default INGRESS 65534 0.0.0.0/0 icmp False
default-allow-internal default INGRESS 65534 10.128.0.0/9 tcp:0-65535,udp:0-65535,icmp False
default-allow-rdp default INGRESS 65534 0.0.0.0/0 tcp:3389 False
default-allow-ssh default INGRESS 65534 0.0.0.0/0 tcp:22 False
pg-from-tag1-to-tag2 default INGRESS 1000 4.3.2.1 tcp:5432 tag1 tag2 False
Run Code Online (Sandbox Code Playgroud)
要打开从每个源到每个实例的默认 PostgreSQL 端口 (5432):
$ gcloud compute firewall-rules create \
postgres-all \
--network default \
--priority 1000 \
--direction ingress \
--action allow \
--rules tcp:5432 \
Run Code Online (Sandbox Code Playgroud)
要在您的计算机(来源YOUR_IP:)和 GCE 实例(目的地:)之间进行限制INSTANCE_IP:
$ gcloud compute firewall-rules create \
postgres-from-you-to-instance \
--network default \
--priority 1000 \
--direction ingress \
--action allow \
--rules tcp:5432 \
--destination-ranges INSTANCES_IP \
--source-ranges YOUR_IP \
Run Code Online (Sandbox Code Playgroud)
也可以使用源和目标网络标签或服务帐户来代替--source-ranges和--destination-ranges。请参阅防火墙文档中的“源或目标”部分。
默认情况下,PostgreSQL 配置为绑定到“localhost”,因此需要更新以下配置文件:
postgresql.conf, 和
pg_hba.conf
可以从 PostgreSQL 本身查询这两个文件的位置(技巧来自此 Stackoverflow 线程):
$ nc -zv 4.3.2.1 5432
Run Code Online (Sandbox Code Playgroud)
postgresql.conf配置文件附带有用的提示,以使其正常工作:
listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
Run Code Online (Sandbox Code Playgroud)
对于快速而肮脏的解决方案,只需将其更改为
listen_addresses = '*'
Run Code Online (Sandbox Code Playgroud)
重新启动服务器(参见此处如何操作)。一旦 PostgreSQL 重新启动,它将开始侦听所有 IP 地址(请参阅 参考资料netstat -tuplen)。
要重新启动 PostgreSQL:
$ sudo systemctl restart postgresql@11-main
# or
$ pg_ctl restart
Run Code Online (Sandbox Code Playgroud)
该listen_addresses文件说,它“指定TCP / IP地址(ES)该服务器监听来自客户端应用程序的连接。 ”,但仅此而已。它指定从中接受数据包的套接字,但如果传入的连接未经过身份验证(通过 配置pg_hba.conf),则无论如何数据包都将被拒绝(丢弃?)。
pg_hba.conf从20.1。pg_hba.conf 文件:“客户端身份验证由配置文件控制,该文件传统上命名pg_hba.conf并存储在数据库集群的数据目录中。(HBA 代表基于主机的身份验证。) ”
这是一个复杂的主题,因此阅读文档至关重要,但这足以在受信任的网络上进行开发:
host all all 0.0.0.0/0 trust
host all all ::/0 trust
Run Code Online (Sandbox Code Playgroud)
此时需要再次重启。
| 归档时间: |
|
| 查看次数: |
6238 次 |
| 最近记录: |