# DuckDB

> For the complete documentation index, see [llms.txt](https://kotlin.github.io/dataframe/llms.txt).

Kotlin DataFrame supports reading from [DuckDB](https://duckdb.org/) databases using JDBC.

API reference: [DuckDb](https://kotlin.github.io/dataframe/api-docs/dataframe-jdbc/org.jetbrains.kotlinx.dataframe.io.db/-duck-db/index.html), [readSqlTable](https://kotlin.github.io/dataframe/api-docs/dataframe-jdbc/org.jetbrains.kotlinx.dataframe.io/read-sql-table.html)

This requires the [dataframe-jdbc module](modules.html#dataframe-jdbc), which is included by default in the general [dataframe artifact](modules.html#dataframe-general) and in [%use dataframe](setupkotlinnotebook.html#integrate-kotlin-dataframe) for Kotlin Notebook.

You’ll also need [the official DuckDB JDBC driver](https://duckdb.org/docs/stable/clients/java):

Gradle project:

```KOTLIN
dependencies {
    implementation("org.duckdb:duckdb_jdbc:$version")
}
```

Kotlin Notebook:

```KOTLIN
USE {
    dependencies("org.duckdb:duckdb_jdbc:$version")
}
```

The actual Maven Central driver version can be found [here](https://mvnrepository.com/artifact/org.duckdb/duckdb_jdbc).

## Read

A [DataFrame](dataframe.html) instance can be loaded from a database in several ways:

a user can read data from a SQL table by a given name ([readSqlTable](readsqldatabases.html)),

as the result of a user-defined SQL query ([readSqlQuery](readsqldatabases.html)),

or from a given `ResultSet` ([readResultSet](readsqldatabases.html)).

It is also possible to load all data from non-system tables, each into a separate `DataFrame` ([readAllSqlTables](readsqldatabases.html)).

See [Read from SQL databases](readsqldatabases.html) for more details.

```KOTLIN
val url = "jdbc:duckdb:/testDatabase"
val username = "duckdb"
val password = "password"

val dbConfig = DbConnectionConfig(url, username, password)

val tableName = "Customer"

val df = DataFrame.readSqlTable(dbConfig, tableName)
```

### Extensions

DuckDB has a special trick up its sleeve: it has support for [extensions](https://duckdb.org/docs/stable/extensions/overview). These can be installed, loaded, and used to connect to a different database via DuckDB. See [Core Extensions](https://duckdb.org/docs/stable/core_extensions/overview) for a list of available extensions.

For example, let's load a dataframe from [Apache Iceberg via DuckDB](https://duckdb.org/docs/stable/core_extensions/iceberg/overview.html), as Iceberg is an unsupported data source in DataFrame at the moment:

```KOTLIN
// Creating an in-memory DuckDB database
val connection = DriverManager.getConnection("jdbc:duckdb:")
val df = connection.use { connection ->
    // install and load Iceberg
    connection.createStatement().execute("INSTALL iceberg; LOAD iceberg;")

    // query a table from Iceberg using a specific SQL query
    DataFrame.readSqlQuery(
        connection = connection,
        sqlQuery = "SELECT * FROM iceberg_scan('data/iceberg/lineitem_iceberg', allow_moved_paths = true);",
    )
}
```

As you can see, the process is very similar to reading from any other JDBC database, just without needing explicit DataFrame support.

