#' PostgreSQL - Create a connection object #' @details Ühenduse loomine PostgreSql andmebaasiga. #' @param host Name or the numeric IPaddress of the host to connect to. #' @param dbname The database name. #' @param user PostgreSQL user name to connect as. Defaults to be the same as the operating system name of the user running the application. #' @param password Password to be used if the server demands password authentication. #' @param port Port number to connect to at the server host. #' @param type Connection type "RPostgres" or "RPostgreSQL". RPostgres is default. #' @return return message. #' @seealso \code{\link{myPostgresConnect}}, \code{\link{myPostgresDropTable}}, #' \code{\link{myPostgresCreateGisDb}}, \code{\link{myPostgresImprotShp}} #' @examples \dontrun{ #' con <- myPostgresConnect(host = '192.168.255.26', port = 5432, #' user = 'osm', password = 'osm', dbname = 'shp') #' #' } #' #' @export #' myPostgresConnect <- function(host = '192.168.255.26', port = 5432, user = 'osm', password = 'osm', dbname = 'shp', type = c("RPostgres", "RPostgreSQL")) { con = NULL if(length(type)>1) type <- type[1] driver <- RPostgres::Postgres() if(tolower(type)=="rpostgresql") driver <- RPostgreSQL::PostgreSQL() try(con <- DBI::dbConnect(driver, host = host, port = port, user = user, password = password, dbname = dbname), silent = FALSE) # Check is connection valid # DBI::dbIsValid(con) - see ei toimi RPostgreSQL ühenduse korral. if(is.null(con)){ message(paste0("DB connection is not valid!")) return() } con }