无法连接到 EC2 实例上的 PostgreSQL DB 运行
cannot connect to PostgreSQL DB running on EC2 instance
我在 EC2 实例上有一个简单的 PostgreSQL 数据库 运行。
ubuntu@ip-172-31-38-xx:~$ service postgresql status
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Fri 2020-06-19 14:04:12 UTC; 7h ago
Main PID: 11065 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 1152)
CGroup: /system.slice/postgresql.service
Jun 19 14:04:12 ip-172-31-38-xx systemd[1]: Starting PostgreSQL RDBMS...
Jun 19 14:04:12 ip-172-31-38-xx systemd[1]: Started PostgreSQL RDBMS.
ubuntu@ip-172-31-38-xx:~$ psql -U postgres
Password for user postgres:
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type "help" for help.
postgres=# SELECT *
postgres-# FROM pg_settings
postgres-# WHERE name = 'port';
name | setting | unit | category | short_desc | extra_desc | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | sourcefile | sourceline | pending_restart
------+---------+------+------------------------------------------------------+------------------------------------------+------------+------------+---------+--------------------+---------+---------+----------+----------+-----------+-----------------------------------------+------------+-----------------
port | 5432 | | Connections and Authentication / Connection Settings | Sets the TCP port the server listens on. | | postmaster | integer | configuration file | 1 | 65535 | | 5432 | 5432 | /etc/postgresql/10/main/postgresql.conf | 63 | f
(1 row)
与此 EC2 实例关联的唯一安全组具有完全开放的入站规则:
5432, TCP, 0.0.0.0/0
但是当我使用客户端连接到此数据库并输入正确的主机名(public IP/DNS)、端口号、数据库名称、用户名和密码时,它总是说:
could not connect to server: Connection refused, is the server running on host "ec2-dns.com(172.public.ip)" and accepting TCP/IP connections on port 5432?
您确定 PostgreSQL 正在侦听您用作主机和端口参数的 IP 地址和端口号吗?尝试修改您的 postgresql.conf 文件并重新启动服务器。
sudo nano /etc/postgresql/{YOUR_POSTGRES_VERSION}/main/postgresql.conf
现在继续查找连接设置并更新以下值。
listen_addresses = {YOUR_IP_ADDRESS}
port = {YOUR_PORT_NUMBER}
现在保存文件并重启 postgresl 服务器:
sudo systemctl restart postgres
签出文档
here:
好吧,我已经从这里想出来了answer
为了让自己能够连接,我做了两件事(完全来自上面的 link,为了方便,我在这里复制它):
- 打开此文件:
sudo vi /etc/postgresql/10/main/pg_hba.conf
紧接此行下方:
host all all 127.0.0.1/32 md5
添加了这一行:
host all all 0.0.0.0/0 md5
- 打开此文件:
sudo vi /etc/postgresql/10/main/postgresql.conf
查找以此开头的行:
#listen_addresses = 'localhost'
通过删除 # 取消注释该行,并将 'localhost' 更改为“*”。
该行现在应如下所示:
listen_addresses = '*' # what IP address(es) to listen on;.
然后重新启动您的服务:
sudo service postgresql restart
那么您应该能够通过 SQL 客户端连接到您的数据库。
我在 EC2 实例上有一个简单的 PostgreSQL 数据库 运行。
ubuntu@ip-172-31-38-xx:~$ service postgresql status
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Fri 2020-06-19 14:04:12 UTC; 7h ago
Main PID: 11065 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 1152)
CGroup: /system.slice/postgresql.service
Jun 19 14:04:12 ip-172-31-38-xx systemd[1]: Starting PostgreSQL RDBMS...
Jun 19 14:04:12 ip-172-31-38-xx systemd[1]: Started PostgreSQL RDBMS.
ubuntu@ip-172-31-38-xx:~$ psql -U postgres
Password for user postgres:
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type "help" for help.
postgres=# SELECT *
postgres-# FROM pg_settings
postgres-# WHERE name = 'port';
name | setting | unit | category | short_desc | extra_desc | context | vartype | source | min_val | max_val | enumvals | boot_val | reset_val | sourcefile | sourceline | pending_restart
------+---------+------+------------------------------------------------------+------------------------------------------+------------+------------+---------+--------------------+---------+---------+----------+----------+-----------+-----------------------------------------+------------+-----------------
port | 5432 | | Connections and Authentication / Connection Settings | Sets the TCP port the server listens on. | | postmaster | integer | configuration file | 1 | 65535 | | 5432 | 5432 | /etc/postgresql/10/main/postgresql.conf | 63 | f
(1 row)
与此 EC2 实例关联的唯一安全组具有完全开放的入站规则:
5432, TCP, 0.0.0.0/0
但是当我使用客户端连接到此数据库并输入正确的主机名(public IP/DNS)、端口号、数据库名称、用户名和密码时,它总是说:
could not connect to server: Connection refused, is the server running on host "ec2-dns.com(172.public.ip)" and accepting TCP/IP connections on port 5432?
您确定 PostgreSQL 正在侦听您用作主机和端口参数的 IP 地址和端口号吗?尝试修改您的 postgresql.conf 文件并重新启动服务器。
sudo nano /etc/postgresql/{YOUR_POSTGRES_VERSION}/main/postgresql.conf
现在继续查找连接设置并更新以下值。
listen_addresses = {YOUR_IP_ADDRESS}
port = {YOUR_PORT_NUMBER}
现在保存文件并重启 postgresl 服务器:
sudo systemctl restart postgres
签出文档 here:
好吧,我已经从这里想出来了answer
为了让自己能够连接,我做了两件事(完全来自上面的 link,为了方便,我在这里复制它):
- 打开此文件:
sudo vi /etc/postgresql/10/main/pg_hba.conf
紧接此行下方:
host all all 127.0.0.1/32 md5
添加了这一行:
host all all 0.0.0.0/0 md5
- 打开此文件:
sudo vi /etc/postgresql/10/main/postgresql.conf
查找以此开头的行:
#listen_addresses = 'localhost'
通过删除 # 取消注释该行,并将 'localhost' 更改为“*”。
该行现在应如下所示:
listen_addresses = '*' # what IP address(es) to listen on;.
然后重新启动您的服务:
sudo service postgresql restart
那么您应该能够通过 SQL 客户端连接到您的数据库。