我有一个运行 PostgreSQL 的本地 Docker 容器。我希望能够从在我的主机(Mac OS)上运行的 R 连接到该数据库并与之交互。
我可以通过以下地址使用 pgadmin4 进行连接
然后添加一个新服务器:
添加新服务器。常规选项卡 --> 名称:标签库。连接选项卡 --> 主机名/地址:postgres。连接选项卡 --> 端口:5432。连接选项卡 --> 维护数据库:postgres。连接选项卡 --> 用户名:tagbase
这完美地工作。
但是,要从 RI 连接,请尝试:
require("RPostgreSQL")
# load the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# create a connection to the postgres database
con <- RPostgreSQL::dbConnect(drv, dbname = "postgres",
host = "localhost", port = 5434,
user = "tagbase", password = "tagbase")
这种尝试只是挂起,直到它使 R 崩溃。
也许可行的解决方案与此类似。非常感谢您的帮助。
编辑 - 20190207
感谢您的评论。我已经做出了没有任何改进的更改,但我同意这些更改是必要的。
我通过终端成功启动了这个 docker 网络(3 个容器),如下所示。在我看来,我想连接到端口 5432 上 0.0.0.0 的 postgres 容器,对吗?
$ docker-compose up
Starting tagbase-server_postgres_1_3f42d4fc1a77 ... done
Starting tagbase-server_pgadmin4_1_52ab92a49f22 ... done
Starting tagbase-server_tagbase_1_9d3a22c8be46 ... done
Attaching to tagbase-server_postgres_1_3f42d4fc1a77, tagbase-server_pgadmin4_1_52ab92a49f22, tagbase-server_tagbase_1_9d3a22c8be46
postgres_1_3f42d4fc1a77 | 2019-02-05 19:35:45.999 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
我以为我正在通过 R 连接到服务器,就像我使用 pgadmin 所做的一样,但以下似乎不起作用:
# create a connection to the postgres database
con <- DBI::dbConnect(RPostgreSQL::PostgreSQL(), dbname = "postgres",
host = "0.0.0.0", port = 5432,
user = "tagbase", password = "tagbase")
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect tagbase@0.0.0.0:5432 on dbname "postgres":
FATAL: role "tagbase" does not exist)
我现在意识到 pgadmin 也在 docker 容器网络中运行。因此,pgadmin 连接的本地主机是数据库服务器。好像我需要这样的解决方案