连接到amazon ec2上的远程postgresql服务器

wha*_*atf 7 postgresql amazon-ec2 amazon-web-services postgresql-9.1

我启动了一个amazon ec2实例,并在其上安装了postgresql 9.1.然后我去了安全组:quicklaunch-1 (there was one more默认`我没有改变)并打开5432 TCP端口,表格如下:

(Service)   Source  Action
22        0.0.0.0/0         Delete
5432      0.0.0.0/32    Delete
5433      0.0.0.0/32    Delete
6432      0.0.0.0/32    Delete
Run Code Online (Sandbox Code Playgroud)

我创建了一个数据库和用户.我/etc/postgresql/9.1/main/pg_hba.conf看起来像这样:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all             0.0.0.0/0               md5
host    db_name         user_name       0.0.0.0/0               md5

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
host    replication     postgres        127.0.0.1/32            md5
host    replication     postgres        ::1/128                 md5
Run Code Online (Sandbox Code Playgroud)

/etc/postgresql/9.1/main/postgresql.conf看起来像这样:

# - Connection Settings -
listen_addresses = '*'
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost', '*' = all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
Run Code Online (Sandbox Code Playgroud)

然后,我尝试连接到远程计算机,如下所示:

psql -h ec2-xxx-xx-xxx-xxx.compute-1.amazonaws.com -d <database_name> -U <username>
Run Code Online (Sandbox Code Playgroud)

其中ec2-xxx-xx-xxx-xxx.compute-1.amazonaws.com是我的公共DNS.

以上命令不会导致任何连接,我该如何连接?

Dan*_*ité 10

在此表中:

5432      0.0.0.0/32    Delete
5433      0.0.0.0/32    Delete
6432      0.0.0.0/32    Delete 
Run Code Online (Sandbox Code Playgroud)

CIDR看起来你不允许任何IP进入.它们不应该0.0.0.0/0像你对端口22(ssh)所拥有的那样吗?


jav*_*eed 6

我找到了这个问题的解决方案。需要做两件事。

  1. 使用文本编辑器修改pg_hba.conf。找到 host all all 127.0.0.1/0 md5 行。在其正下方添加以下新行:host all all 0.0.0.0/0 md5

  2. 编辑 PostgreSQL postgresql.conf 文件:

    使用文本编辑器修改postgresql.conf。找到以 #listen_addresses = 'localhost' 开头的行。通过删除 # 取消注释该行,并将 localhost 更改为*。该行现在应如下所示:listen_addresses = '*' #要侦听的 IP 地址;。

现在只需重新启动你的 postgres 服务,它就会连接