[英]How to connect to PostgreSQL running in Docker container from local R?
我有一個運行 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 崩潰。
也許可行的解決方案與此類似。 非常感謝您的幫助。
感謝您的評論。 我進行了更改但沒有任何改進,但同意這些更改是必要的。
我通過終端成功啟動了這個 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 世界之外直接連接到 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
然后根據提供的說明構建並運行dockers (檢查於 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.