【问题标题】:connect to postgres server on google compute engine连接到谷歌计算引擎上的 postgres 服务器
【发布时间】:2014-08-21 16:24:36
【问题描述】:

我到处搜索过这个,但是搜索了一个半小时后,我没有找到任何相关的东西。

如何连接到我的谷歌计算引擎上的数据库?即我想使用笔记本电脑上的 pgadmin3 连接到在我的谷歌计算引擎上运行的 postgres 服务器。

这甚至可能吗?如果是这样,我该怎么做?

提前致谢!

【问题讨论】:

标签: postgresql google-compute-engine


【解决方案1】:

PostgreSQL must also be configured to allow remote connections,否则连接请求将失败,即使所有防火墙规则都正确且 PostgreSQL 服务器正在侦听正确的端口。

步骤

大纲

无法创建链接,但这是一个相当长的答案,所以这可能会有所帮助。

  1. 在任何步骤中检查端口的工具
    0.1 nc 或 netcat
    0.2 nmap
    0.3 netstat
    0.4 lsof
  2. IP 地址
    1.1 您的笔记本电脑的公网IP地址
    1.2 GCE实例的IP地址
  3. 防火墙规则
    2.1 检查现有
    2.2 添加新的防火墙规则
  4. 配置 PostgreSQL 以接受远程连接
    3.1 查找上述配置文件
    3.2 postgresql.conf
    3.3pg_hba.conf

0。在任何步骤中检查端口的工具

0.1 nc 或 netcat

$ nc -zv 4.3.2.1 5432

在哪里

 -v      Produce more verbose output.

 -z      Only scan for listening daemons, without sending any data to
         them.  Cannot be used together with -l.

可能的结果:

  • Connection to 4.3.2.1port [tcp/postgresql] succeeded!

    是的。

  • nc: connect to 4.3.2.1 port 8000 (tcp) failed: Connection refused

    端口被防火墙打开,但服务没有监听或拒绝连接。

  • 命令只是挂起

    防火墙正在阻止。

0.2 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?

0.3 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    -                   

在哪里

-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.

当在运行 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  

0.4 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)

1。 IP 地址

要从您的笔记本电脑连接,您需要笔记本电脑的公共 IP 地址以及 Google Compute Engine (GCE) 实例的公共 IP 地址。

1.1 您的笔记本电脑的公网IP地址

(来自this article。)

$ dig +short myip.opendns.com @resolver1.opendns.com
4.3.2.1

1.2 GCE实例的IP地址

$ 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

如果您还需要实例的network-tags:

$ 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']

2。防火墙规则

仅处理以下 GCE 防火墙规则,但请确保 iptables 不会无意中阻止流量。

另见

2.1 检查现有

$ 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.

包含网络标签的更全面的列表(来自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

2.2 添加新的防火墙规则

要打开从每个源到每个实例的默认 PostgreSQL 端口 (5432):

$ gcloud compute firewall-rules create \
    postgres-all                       \
    --network default                  \
    --priority 1000                    \
    --direction ingress                \
    --action allow                     \
    --rules tcp:5432                   \

在您的计算机(来源: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            \

除了--source-ranges 和--destination-ranges,还可以使用源和目标网络标签或服务帐户。请参阅"Source or destination" section in the firewall docs。

3。配置 PostgreSQL 以接受远程连接

这是对Neeraj Singh 的post 的更新。

默认情况下,PostgreSQL 被配置为绑定到“localhost”,因此需要更新以下配置文件:

  1. postgresql.conf,和

  2. pg_hba.conf

3.1 找到上述配置文件

这两个文件的位置可以从 PostgreSQL 本身查询(技巧取自this Stackoverflow thread):

$ sudo -u postgres psql -c "SHOW hba_file" -c "SHOW config_file"

3.2postgresql.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)

对于快速而肮脏的解决方案,只需将其更改为

listen_addresses = '*'

重启服务器(见here如何)。一旦 PostgreSQL 重新启动,它将开始侦听所有 IP 地址(请参阅netstat -tuplen)。

重启 PostgreSQL:

$ sudo systemctl restart postgresql@11-main

# or

$ pg_ctl restart

listen_addresses 文档说它“指定服务器用于侦听来自客户端应用程序的连接的 TCP/IP 地址。”,仅此而已。它指定了接收数据包的套接字,但是如果传入的连接没有经过身份验证(通过pg_hba.conf 配置),那么无论如何数据包都将被拒绝(丢弃?)。

3.3 pg_hba.conf

来自20.1. The pg_hba.conf File:“_Client 身份验证由配置文件控制,该文件传统上命名为 pg_hba.conf 并存储在数据库集群的数据目录中。(HBA 代表基于主机的身份验证。)_”

这是一个复杂的话题,因此阅读文档至关重要,但这足以在可信网络上进行开发:

host    all   all   0.0.0.0/0   trust
host    all   all   ::/0        trust

此时需要再次重启。

【讨论】:

    【解决方案2】:

    你需要:

    • 确保 Postgres 正在侦听 TCP 流量(您可以通过连接到您的实例并运行 netstat -ntpl 来检查)。通常,Postgres 会监听 5432 端口。
    • 确保没有本地防火墙阻止实例上 Postgres 端口的流量(您可以运行 iptables -L)
    • 确保没有 GCE 防火墙阻止从您的 IP 到 Postgres 端口上的实例的流量。您应该阅读this documentation page,尤其是“防火墙”部分

    【讨论】:

    • 感谢您为我指明了正确的方向。只需要添加一个相关的防火墙并调整我的 postgresql.conf 和 pg_hba.conf 文件
    • 嗨。我面临同样的问题。我已经更改了我的配置文件并更改了防火墙,但我仍然无法连接。你能帮忙吗?
    猜你喜欢
    • 2018-05-27
    • 2016-08-14
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多