Attempts to connect to a PostgreSQL database. If the connection fails, it will retry a user-specified number of times.

sp_get_postgres_connection(
  user = "postgres",
  password = "postgres",
  dbname = "dvdrental",
  host = "localhost",
  port = 5439,
  seconds_to_test = 30,
  connection_tab = FALSE
)

Arguments

user

character: Username for connecting to the database. The user must exist and be authorized to connect. The default is "postgres".

password

character: Password that corresponds to the username. The password must be correct for the user. The default is "postgres".

dbname

character: The name of the database within the database server. The database must exist, and the user must be authorized to access it. The default is "dvdrental".

host

character: The IP address or host where postgreSQL is located, defaults to "localhost"

port

integer: The port on the host that PostgreSQL listens on, defaults to 5439. Note that this is not the PostgreSQL default port, 5432! Why? If PostgreSQL is running on the host or in another container, it probably has claimed port 5432, since that's its default, and our container won't work! So we need to use a different port for our PostgreSQL container.

seconds_to_test

integer: The number of iterations to try while waiting for PostgreSQL service to be ready. The function sleeps one second between connection attempts, so a value of 10 would require approximately 10 seconds. The default is 30.

connection_tab

logical: sp_get_postgres_connection can open a tab in the RStudio connections pane. connection_tab = FALSE is the default - call with connection_tab = TRUE to open the tab.

Value

If successful: a connection object, which is an S4 object that inherits from DBIConnection, used to communicate with the database engine. If unsuccessful, the function terminates with an error message.

Examples

if (FALSE) con <- sp_get_postgres_connection( user = "postgres", password = "postgres", dbname = "postgres" ) if (FALSE) { build_log <- sp_make_dvdrental_image("test-dvdrental:latest") sp_docker_images_tibble() sp_pg_docker_run( container_name = "test-dvdrental", image_tag = "test-dvdrental:latest", postgres_password = "postgres" ) sp_docker_containers_tibble() connection <- sp_get_postgres_connection( user = "postgres", password = "postgres", dbname = "dvdrental", host = "localhost", port = 5439, seconds_to_test = 30, connection_tab = TRUE ) }