Tôi có một tầng miễn phí vi RHEL 6 Ví dụ chạy và có postgresql 9.2 được cài đặt bằng cách sử dụng hướng dẫn yum ở đây: http://yum.pgrpms.org/howtoyum.phpkhông thể kết nối với PostgreSQL từ xa trên Amazon EC2 dụ sử dụng pgAdmin
Và tôi có thể kết nối đến máy chủ PG sử dụng cục bộ trên máy chủ này:
03:46:20 [email protected][~]$ psql -hlocalhost -p5432 -Upostgres
Tuy nhiên tôi chưa bao giờ kết nối thành công với hộp bên ngoài hộp. Thông báo lỗi trông giống như:
12:11:56 [email protected][~]$ psql -h ec2-xxx.ap-southeast-1.compute.amazonaws.com -p5432 -Upostgres
psql: could not connect to server: Connection refused
Is the server running on host "ec2-54-251-188-3.ap-southeast-1.compute.amazonaws.com" (54.251.188.3) and accepting TCP/IP connections on port 5432?
Tôi đã thử một loạt các cách khác nhau. Đây là cách các file cấu hình của tôi trông bây giờ:
/var/lib/pgsql/9.2/data/postgresql.conf:
...
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
...
/var/lib/pgsql/9.2/data/pg_hba.conf:
# TYPE DATABASE USER ADDRESS METHOD
host all pgadmin 0.0.0.0/24 trust
host all all [my ip]/24 md5
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
Tôi đã cố gắng đặt địa chỉ trên thành 0.0.0.0/0 nhưng id không hoạt động.
Và mỗi khi tôi làm một sự thay đổi tôi khởi động lại bằng cách chạy này
service postgresql-9.2 restart
Trong Nhóm Bảo Mật Của Những ví dụ EC2 này tôi có thể thấy quy tắc này đã:
TCP
Port (Service) Source Action
22 (SSH) 0.0.0.0/0 Delete
80 (HTTP) 0.0.0.0/0 Delete
5432 0.0.0.0/0 Delete
Các lệnh netstat cho thấy cảng đã mở:
04:07:46 [email protected][~]$ netstat -na|grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp 0 0 :::5432 :::* LISTEN
unix 2 [ ACC ] STREAM LISTENING 14365 /tmp/.s.PGSQL.5432
Để trả lời câu hỏi của bma:
Nếu tôi chạy lệnh nmap trên máy chủ cục bộ nó có vẻ gợi ý rằng qua DNS nội bộ nó sẽ đến một máy chủ ở đâu 5432 mở cửa:
10:16:05 [email protected][~]$ nmap -Pnv -p 5432 ec2-54-251-188-3.ap-southeast-1.compute.amazonaws.com
Starting Nmap 5.51 (http://nmap.org) at 2013-07-22 10:16 EDT
Nmap scan report for ec2-54-251-188-3.ap-southeast-1.compute.amazonaws.com (172.31.26.139)
Host is up (0.00012s latency).
rDNS record for 172.31.26.139: ip-172-31-26-139.ap-southeast-1.compute.internal
PORT STATE SERVICE
5432/tcp open postgresql
Nmap done: 1 IP address (1 host up) scanned in 0.07 seconds
Và lệnh iptables cho đầu ra sau đây
10:16:14 [email protected][~]$ iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
25776 14M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
45 1801 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
251 15008 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
35 2016 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 21695 packets, 5138K bytes)
pkts bytes target prot opt in out source destination
[Sửa sau khi thêm theo gợi ý của BMA]
iptables trông như thế này sau khi bổ sung mới:
11:57:20 [email protected][~]$ iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
26516 14M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
47 1885 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
255 15236 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
38 2208 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
0 0 ACCEPT tcp -- * * [my ip] 54.251.188.3 tcp spts:1024:65535 dpt:5432 state NEW,ESTABLISHED
0 0 ACCEPT tcp -- * * 0.0.0.0/0 54.251.188.3 tcp spt:5432 dpts:1024:65535 state ESTABLISHED
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 5 packets, 1124 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT tcp -- * * 54.251.188.3 [my ip] tcp spt:5432 dpts:1024:65535 state ESTABLISHED
0 0 ACCEPT tcp -- * * 54.251.188.3 0.0.0.0/0 tcp spts:1024:65535 dpt:5432 state NEW,ESTABLISHED
Nhưng tôi vẫn không thể kết nối (cùng một lỗi). Những gì có thể là phần còn thiếu ở đây?
Tôi gặp sự cố tương tự. Giải quyết nó bằng cách sử dụng thông tin trên http://stackoverflow.com/questions/14545298/connect-to-remote-postgresql-server-on-amazon-ec2 –