简体   繁体   English

无法使用 pgAdmin 将 PostgreSQL 连接到远程数据库

[英]Unable to connect PostgreSQL to remote database using pgAdmin

I installed PostgreSQL on my local server (Ubuntu) with IP 192.168.1.10.我在我的本地服务器 (Ubuntu) 上安装了 PostgreSQL,IP 为 192.168.1.10。 Now, I'm trying to access the database from my client machine (Ubuntu) with IP 192.168.1.11 with pgAdmin.现在,我正在尝试使用 pgAdmin 从我的客户端机器 (Ubuntu) 访问数据库,IP 为 192.168.1.11。

I know I have to make changes in postgresql.conf and pg_hba.conf to allow the client to connect.我知道我必须在 postgresql.conf 和 pg_hba.conf 中进行更改以允许客户端连接。 Could you please guide me?你能指导我吗?

It is actually a 3 step process to connect to a PostgreSQL server remotely through pgAdmin3.通过 pgAdmin3 远程连接到 PostgreSQL 服务器实际上是一个 3 步过程。

Note: I use Ubuntu 11.04 and PostgreSQL 8.4.注意:我使用 Ubuntu 11.04 和 PostgreSQL 8.4。

  1. You have to make PostgreSQL listening for remote incoming TCP connections because the default settings allow to listen only for connections on the loopback interface.您必须让 PostgreSQL 侦听远程传入 TCP 连接,因为默认设置只允许侦听环回接口上的连接。 To be able to reach the server remotely you have to add the following line into the file /etc/postgresql/8.4/main/postgresql.conf:为了能够远程访问服务器,您必须将以下行添加到文件/etc/postgresql/8.4/main/postgresql.conf:

    listen_addresses = '*'监听地址 = '*'

  2. PostgreSQL by default refuses all connections it receives from any remote address, you have to relax these rules by adding this line to /etc/postgresql/8.4/main/pg_hba.conf:默认情况下,PostgreSQL 拒绝从任何远程地址接收的所有连接,您必须通过将此行添加到/etc/postgresql/8.4/main/pg_hba.conf:来放宽这些规则/etc/postgresql/8.4/main/pg_hba.conf:

    host all all 0.0.0.0/0 md5托管所有所有 0.0.0.0/0 md5

    This is an access control rule that let anybody login in from any address if he can provide a valid password (the md5 keyword).这是一个访问控制规则,只要他能提供有效的密码(md5 关键字),任何人都可以从任何地址登录。 You can use needed network/mask instead of 0.0.0.0/0 .您可以使用需要的网络/掩码而不是 0.0.0.0/0 。

  3. When you have applied these modifications to your configuration files you need to restart PostgreSQL server.将这些修改应用到配置文件后,您需要重新启动 PostgreSQL 服务器。 Now it is possible to login to your server remotely, using the username and password.现在可以使用用户名和密码远程登录到您的服务器。

If you're using PostgreSQL 8 or above, you may need to modify the listen_addresses setting in /etc/postgresql/8.4/main/postgresql.conf .如果您使用的是 PostgreSQL 8 或更高版本,则可能需要修改/etc/postgresql/8.4/main/postgresql.conflisten_addresses设置。

Try adding the line:尝试添加以下行:

listen_addresses = *

which will tell PostgreSQL to listen for connections on all network interfaces.这将告诉 PostgreSQL 侦听所有网络接口上的连接。

If not explicitly set, this setting defaults to localhost which means it will only accept connections from the same machine.如果没有明确设置,这个设置默认为localhost ,这意味着它只接受来自同一台机器的连接。

In linux terminal try this:在 linux 终端试试这个:

  • sudo service postgresql start : to start the server sudo service postgresql start : 启动服务器
  • sudo service postgresql stop : to stop thee server sudo service postgresql stop : 停止你的服务器
  • sudo service postgresql status : to check server status sudo service postgresql status : 检查服务器状态

I didn't have to change my prostgresql.conf file but, i did have to do the following based on my psql via command line was connecting and pgAdmin not connecting on RDS with AWS.我不必更改我的 prostgresql.conf 文件,但是,我确实必须根据我的 psql 通过命令行执行以下操作,并且 pgAdmin 未在 RDS 上与 AWS 连接。

I did have my RDS set to Publicly Accessible.我确实将 RDS 设置为公开访问。 I made sure my ACL and security groups were wide open and still problem so, I did the following: sudo find . -name *.conf我确保我的 ACL 和安全组是完全开放的,但仍然存在问题,因此,我执行了以下操作: sudo find . -name *.conf sudo find . -name *.conf then sudo nano ./data/pg_hba.conf then added to top of directives in pg_hba.conf file host all all 0.0.0.0/0 md5 and pgAdmin automatically logged me in. sudo find . -name *.conf然后sudo nano ./data/pg_hba.conf然后添加到 pg_hba.conf 文件host all all 0.0.0.0/0 md5和 pgAdmin 自动登录我的指令的顶部。

This also worked in pg_hba.conf file host all all md5 without any IP address and this also worked with my IP address host all all <myip>/32 md5这也适用于 pg_hba.conf 文件host all all md5没有任何 IP 地址,这也适用于我的 IP 地址host all all <myip>/32 md5

As a side note, my RDS was in my default VPC.作为旁注,我的 RDS 在我的默认 VPC 中。 I had an identical RDS instance in my non-default VPC with identical security group, ACL and security group settings to my default VPC and I could not get it to work.我的非默认 VPC 中有一个相同的 RDS 实例,其安全组、ACL 和安全组设置与我的默认 VPC 相同,但我无法让它工作。 Not sure why but, that's for another day.不知道为什么,但那是另一天。

Connecting to PostgreSQL via SSH Tunneling通过 SSH 隧道连接到 PostgreSQL

In the event that you don't want to open port 5432 to any traffic, or you don't want to configure PostgreSQL to listen to any remote traffic, you can use SSH Tunneling to make a remote connection to the PostgreSQL instance.如果您不想向任何流量开放端口 5432,或者您不想配置 PostgreSQL 以侦听任何远程流量,则可以使用 SSH 隧道与 PostgreSQL 实例建立远程连接。 Here's how:就是这样:

  1. Open PuTTY.打开腻子。 If you already have a session set up to connect to the EC2 instance, load that, but don't connect to it just yet.如果您已经设置了一个会话以连接到 EC2 实例,请加载该会话,但不要立即连接到它。 If you don't have such a session, see this post.如果您没有这样的会话,请参阅此帖子。
  2. Go to Connection > SSH > Tunnels转至连接 > SSH > 隧道
  3. Enter 5433 in the Source Port field.在源端口字段中输入 5433。
  4. Enter 127.0.0.1:5432 in the Destination field.在目的地字段中输入 127.0.0.1:5432。
  5. Click the "Add" button.单击“添加”按钮。
  6. Go back to Session, and save your session, then click "Open" to connect.返回会话,并保存您的会话,然后单击“打开”进行连接。
  7. This opens a terminal window.这将打开一个终端窗口。 Once you're connected, you can leave that alone.连接后,您可以不理会它。
  8. Open pgAdmin and add a connection.打开 pgAdmin 并添加一个连接。
  9. Enter localhost in the Host field and 5433 in the Port field.在主机字段中输入 localhost,在端口字段中输入 5433。 Specify a Name for the connection, and the username and password.指定连接的名称以及用户名和密码。 Click OK when you're done.完成后单击“确定”。

For redhat linux对于红帽 linux

sudo vi /var/lib/pgsql9/data/postgresql.conf 

pgsql9 is the folder for the postgres version installed, might be different for others pgsql9 是安装的 postgres 版本的文件夹,其他人可能会有所不同

changed listen_addresses = '*' from listen_addresses = 'localhost' and then从 listen_addresses = 'localhost' 改变了 listen_addresses = '*' 然后

sudo /etc/init.d/postgresql stop
sudo /etc/init.d/postgresql start

Check your firewall.检查您的防火墙。 When you disable it, then you can connect.当您禁用它时,您就可以连接了。 If you want/can't disable the firewall, add a rule for your remote connection.如果您想要/不能禁用防火墙,请为您的远程连接添加规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM