---
sidebar_position: 3
title: "Connect from Java via Postgres endpoint"
sidebar_label: Java (JDBC)
description: Connect to MotherDuck from Java using the PostgreSQL JDBC driver via the Postgres wire protocol
feature_stage: preview
---

You can query MotherDuck from Java using the standard [PostgreSQL JDBC driver](https://jdbc.postgresql.org/) — no DuckDB installation required.

For connection parameters, SSL options, and limitations, see the [Postgres Endpoint reference](/sql-reference/postgres-endpoint).

## Prerequisites

You'll need a [MotherDuck access token](/key-tasks/authenticating-and-connecting-to-motherduck/authenticating-to-motherduck). Set it as an environment variable:

```bash
export MOTHERDUCK_TOKEN="your_token_here"
```

Add the PostgreSQL JDBC driver to your project:

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs>
<TabItem value="maven" label="Maven">

```xml
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.7.5</version>
</dependency>
```

</TabItem>
<TabItem value="gradle" label="Gradle">

```groovy
implementation 'org.postgresql:postgresql:42.7.5'
```

</TabItem>
</Tabs>

## Connect

```java
import java.sql.*;

public class MotherDuckExample {
    public static void main(String[] args) throws SQLException {
        String token = System.getenv("MOTHERDUCK_TOKEN");
        String url = "jdbc:postgresql://pg.us-east-1-aws.motherduck.com:5432/md:"
                   + "?sslmode=verify-full"
                   + "&sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory";

        try (Connection conn = DriverManager.getConnection(url, "postgres", token);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(
                 "SELECT title, score FROM sample_data.hn.hacker_news WHERE type='story' LIMIT 10")) {

            ResultSetMetaData meta = rs.getMetaData();
            int columnCount = meta.getColumnCount();

            while (rs.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    System.out.print(meta.getColumnName(i) + "=" + rs.getString(i));
                    if (i < columnCount) System.out.print(", ");
                }
                System.out.println();
            }
        }
    }
}
```

You can also configure the connection using a `Properties` object:

```java
import java.sql.*;
import java.util.Properties;

Properties props = new Properties();
props.setProperty("user", "postgres");
props.setProperty("password", System.getenv("MOTHERDUCK_TOKEN"));
props.setProperty("sslmode", "verify-full");
props.setProperty("sslfactory", "org.postgresql.ssl.DefaultJavaSSLFactory");

Connection conn = DriverManager.getConnection(
    "jdbc:postgresql://pg.us-east-1-aws.motherduck.com:5432/md:",
    props
);
```

## SSL notes

The PostgreSQL JDBC driver looks for a root certificate at `~/.postgresql/root.crt` by default. To use your JVM's built-in truststore instead (which includes standard CAs like Let's Encrypt), set `sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory`.

If certificate verification doesn't work in your environment, you can fall back to `sslmode=require`, which encrypts the connection but doesn't verify the server certificate.

For more details on SSL options, see [SSL and certificate verification](/sql-reference/postgres-endpoint#ssl-and-certificate-verification).
