Tutorials  /  Data Management  /  Connect Data

How to connect to CARTO from external applications using JDBC

Connecting to CARTO from external applications using JDBC.

This tutorial shows to connect with the CARTO database using the ODBC connection. We would strongly recommend reading the SQL direct connection article to know more about this feature before reading this one.

This tutorial assumes that you have already installed a Java Runtime Environment in your computer, version 8 or newer.

Configuration

  • Download the PostgreSQL JDBC driver from https://jdbc.postgresql.org/.
  • Add the downloaded JAR file to your classpath
  • Create a JDBC connection in your Java program including the certificate files in the connection properties. We must use the DER PKCS #8 private key file.
String url = "jdbc:postgresql://{hostname}:{port}/cartodb";

Properties props = new Properties();
props.setProperty("user","username");
props.setProperty("password","API key");
props.setProperty("ssl","true");
props.setProperty("sslmode","verify-full");
props.setProperty("sslcert","./client.crt");
props.setProperty("sslkey","./client.key.pk8");
props.setProperty("sslrootcert","./server_ca.pem");

Connection conn = DriverManager.getConnection(url, props);

System.out.println("Connection created");
conn.close();
System.out.println("Connection closed");