Basic Connection to Vertica cluster

I am trying to establish a connectio to Vertica in order to run a simple query for testing the successfull connection. Although the my R script below seems to run properly, the connection fails and the sql query does not run.

library(RJDBC)

# Path to the uploaded Vertica JDBC driver JAR file
jdbc_driver_path <- "/cloud/project/vertica-jdbc-24.2.0-1.jar"

# Load the JDBC driver
drv <- JDBC(driverClass = "com.vertica.jdbc.Driver", classPath = jdbc_driver_path)

# Database connection details
jdbc_url <- "jdbc:vertica://"my_host (ip of one node of my cluster)":my_port(5450)/my_database_name(verticadb)"
username <- "my_username"
password <- "my_password"

# Try to establish the connection and catch any errors
conn <- tryCatch({
  dbConnect(drv, jdbc_url, username, password)
}, error = function(e) {
  cat("Error: ", e$message, "\n")
  NULL
})

# Check if the connection was successful
if (!is.null(conn)) {
  # Execute a query
  result <- dbGetQuery(conn, "SELECT * FROM shema_name.table_name LIMIT 10")
  
  # Print the result
  print(result)
  
  # Close the connection
  dbDisconnect(conn)
} else {
  cat("Failed to connect to the database.\n")
}

The error appears is 

Failed to connect to /"my_host"
Error:  Unable to connect JDBC to jdbc:vertica://"my_host":5450/verticadb
  JDBC ERROR: [Vertica][VJDBC](100176) Failed to connect to host "my_host" on port 5450. Reason: Failed to establish a connection to the primary server or any backup address due to network error 

Anyone that has any suggestions?
My final goal is the data migration from a database to Vertica
Thanks in advance!

Hi @Nikitas91, welcome to the forum!

Because you're having trouble at the connection step, I'm wondering if your Vertica database has a firewall that needs to allow connections from Posit Connect.

Can you connect to the Vertica database from any arbitrary location? Meaning, not when you are on a VPN or OKTA or something, but could you use your script to connect from a non-work computer?

Best,
Randy

Thank you very much for your immediate response.
Actually, in order to connect to my vertica i have to establish a VPN connection.
Otherwise i cannot have access.
Although when i run my script in posit cloud i have already established my VPN connection.
I guess that doesn't solve my problem?

1 Like

Yeah, I think that's your problem. Conceptually, the permission that allows you to sign into Posit Cloud and the permission that lets Posit Cloud connect to Vertica are different.

It's probably the case that Vertica needs to allow the Posit Cloud IPs as allowable incoming connections.

The Posit Cloud documentation enumerates the IP addresses that would need to be able to access any private resources you have:

https://docs.posit.co/cloud/guide/data/#access-data-behind-a-firewall

Best,
Randy