I have a local Docker container running PostgreSQL. I want to be able to connect to and interact with this database from R running on my host machine (Mac OS).
I can connect using pgadmin4 via the following address
then adding a new server:
Add new server. General Tab --> name: tagbase. Connection Tab --> Host name/address: postgres. Connection Tab --> Port: 5432. Connection Tab --> Maintenance database: postgres. Connection Tab --> Username: tagbase
This works perfectly.
However, to connect from R I try:
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")
This attempt simply hangs until it crashes R.
Perhaps a viable solution is something similar to this. Many thanks for any help.
EDIT - 20190207
Thanks for the comments. I have made the changes with no improvement but agreed the changes were necessary.
I successfully start this docker network (of 3 containers) via terminal as below. It looks to me like I want to connect to the postgres container at 0.0.0.0 on port 5432, correct?
$ 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
I thought I was connecting to the server via R exactly as I've done using pgadmin but the following doesn't seem to work:
# 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 [email protected]:5432 on dbname "postgres":
FATAL: role "tagbase" does not exist)
I now realize pgadmin is also running in the docker container network. Thus, local host for the pgadmin connection is the database server. Seems like I need a solution like this
Note the source for the docker builds is here following the instructions here.
library(DBI); con <- DBI::dbConnect(RPostgreSQL::PostgreSQL(), ...), though the differences are minor. It might be that the docker port binding is not on localhost, have you tried other addresses? I'm not accustomed to using0.0.0.0in this way, but does that work? (I know what0.0.0.0means, but I've always seen it as "bind to any/all interfaces", not which interface to bind to.)library, notrequire. The latter never stops following code when the package is not available, which is almost never what is intended. Refs: stackoverflow.com/a/51263513/3358272. Put another way: if you wantrequire, capture and inspect its return value. If you do not check the return and your follow-on code depends on its functions, then uselibrary. (If your follow-on code does not depend on its return ... then something else is going on.)docker run -poption or Docker Composeports:section, for the database itself. With what you’ve shown you’re trying to connect to PGAdmin as though it were the database;port=5432would be more typical.