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 tagbase@0.0.0.0: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.