3

我有一个运行 PostgreSQL 的本地 Docker 容器。我希望能够从在我的主机(Mac OS)上运行的 R 连接到该数据库并与之交互。

我可以通过以下地址使用 pgadmin4 进行连接

http://0.0.0.0:5434/browser/

然后添加一个新服务器:

添加新服务器。常规选项卡 --> 名称:标签库。连接选项卡 --> 主机名/地址: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 连接的本地主机是数据库服务器。好像我需要这样的解决方案

请注意,docker 构建的源代码在此处,按照此处的说明进行操作。

4

1 回答 1

0

如果您想从外部 docker 世界直接连接到 docker 内的 postgres 数据库,则必须在 postgres docker 上公开一个端口。所以首先,你需要编辑文件“Dockerfile-postgres”,并添加EXPOSE 5432

FROM postgres:10

COPY ./sqldb/tagbase-schema.sql /docker-entrypoint-initdb.d/
# Expose default postgres port
EXPOSE 5432

然后根据提供的说明构建并运行docker(检查于 2019 年 10 月 6 日)

$ docker-compose build
$ docker-compose up

使用 pgAdmin 添加数据库

添加新服务器  常规选项卡 --> 名称:tagbase  连接选项卡 --> 主机名/地址:postgres  连接选项卡 --> 端口:5432  连接选项卡 --> 维护数据库:postgres  连接选项卡 --> 用户名: 标签库

根据数据库名称和端口编辑您的 R 脚本:

# install.packages('RPostgreSQL')
library(RPostgreSQL)

# load the PostgreSQL driver
drv <- dbDriver("PostgreSQL")

# create a connection to the postgres database
con <- RPostgreSQL::dbConnect(drv, dbname = "tagbase",
                              host = "localhost", port = 5432,
                              user = "tagbase", password = "tagbase")

# Test query
temp <- dbGetQuery(con, 'select * from public.metadata_types')

# Evaluate output
str(temp)
# 'data.frame': 142 obs. of  8 variables:
#   $ attribute_id  : num  1 2 3 4 5 6 7 8 9 10 ...
# $ category      : chr  "instrument" "instrument" "instrument" "instrument" ...
# $ attribute_name: chr  "instrument_name" "instrument_type" "firmware" "manufacturer" ...
# $ type          : chr  "string" "string" "string" "string" ...
# $ description   : chr  "Append an identifer that is unique within your organization. This is essential if a device is recycled." "Type of instrument" "Version number of the firmware used to build the device" "Name of manufacturer" ...
# $ example       : chr  "16P0100-Refurb2" "archival, popup, satellite, acoustic tag, or acoustic receiver" NA "Wildlife Computers, Microwave Telemetry, Lotek Wireless, Desert Star Systems, CEFAS, StarOddi, Sea Mammal Resea"| __truncated__ ...
# $ comments      : chr  "Devices might be reused, so the serial number will be the same. The only way to distinguish is by providing a u"| __truncated__ "Should be restricted to the examples provided." NA NA ...
# $ necessity     : chr  "required" "required" "required" "required" ...

# Disconnect from database
dbDisconnect(con)
于 2019-10-06T14:46:00.973 回答