连接多个服务器 PostgreSQL R

Connecting multiple server PostgreSQL R

我需要将一个数据库的 3 个分片放在三个不同的服务器上。所以我在 pgAdmin(s1,s2,s3) 中创建了 3 个服务器,然后我将每个服务器放在一个分片中。然后,我尝试连接 R 中的其中一台服务器;但是,我无法建立联系。我总是收到错误消息:

postgresqlNewConnection(drv, ...) 错误:RS-DBI 驱动程序:(无法连接 dbname "postgres" 上的 postgres@172.17.0.1:5432:无法连接到服务器:操作超时是主机“172.17.0.1”上的服务器 运行 并在端口 5432 上接受 TCP/IP 连接?

我的代码是:

#install.packages("RPostgreSQL")
require("RPostgreSQL")
library(DBI)

# create a connection
# save the password that we can "hide" it as best as we can by collapsing it
pw <- {
  "postgres"
}

# loads the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# creates a connection to the postgres database
con <- dbConnect(
  drv,
  dbname = "postgres",
  host = "172.17.0.1",
  port = 5432,
  user = "postgres",
  password = pw
)
rm(pw) # removes the password

pgAdmin snap

我是不是写错了什么?

如果这是使用容器,请确保在 0.0.0.0 上转发端口 5432,即容器正在侦听端口 5432。

此外,如果您不是仅在本地进行连接,则必须检查此设置>,在 postgresql.conf 文件中:

# - Connection Settings -
#listen_addresses = 'localhost'  >>>> This should be = '*'  instead of localhost

保存配置并重启服务。希望这对您有所帮助!