Skip to main content

Overview

The DuckDB class represents a database instance and holds all database-specific metadata, including the catalog, transaction manager, and storage. Each database instance can be accessed by multiple Connection objects. Defined in: duckdb/main/database.hpp (line 106)
class DuckDB {
public:
    explicit DuckDB(const char *path = nullptr, DBConfig *config = nullptr);
    explicit DuckDB(const string &path, DBConfig *config = nullptr);
    explicit DuckDB(DatabaseInstance &instance);
    ~DuckDB();
    
    shared_ptr<DatabaseInstance> instance;
};

Constructors

Default Constructor

DuckDB(const char *path = nullptr, DBConfig *config = nullptr)
Creates a new DuckDB database instance.
path
const char*
default:"nullptr"
Path to the database file. If nullptr, creates an in-memory database.
config
DBConfig*
default:"nullptr"
Database configuration options. If nullptr, uses default configuration.
Examples:
// Create an in-memory database
DuckDB db(nullptr);

Constructor with Configuration

DuckDB(const char *path, DBConfig *config)
Creates a database with custom configuration. Example:
DBConfig config;
config.options.access_mode = AccessMode::READ_ONLY;
config.options.maximum_memory = 1024 * 1024 * 1024; // 1GB

DuckDB db("my_database.db", &config);

Instance Constructor

DuckDB(DatabaseInstance &instance)
Creates a database from an existing DatabaseInstance.
instance
DatabaseInstance&
An existing database instance to wrap.

Member Variables

instance

shared_ptr<DatabaseInstance> instance
instance
shared_ptr<DatabaseInstance>
Reference to the actual database instance. This shared pointer allows multiple DuckDB objects to reference the same underlying database.

Methods

LoadStaticExtension

template <class T>
void LoadStaticExtension()
Loads a statically linked extension by its class type. Example:
DuckDB db("my_database.db");
db.LoadStaticExtension<MyExtension>();

GetFileSystem

FileSystem &GetFileSystem()
return
FileSystem&
Reference to the file system used by this database.
Returns the file system instance associated with the database.

NumberOfThreads

idx_t NumberOfThreads()
return
idx_t
Number of threads configured for this database instance.
Returns the number of threads available for parallel query execution. Example:
DuckDB db(nullptr);
auto num_threads = db.NumberOfThreads();
std::cout << "Using " << num_threads << " threads" << std::endl;

ExtensionIsLoaded

bool ExtensionIsLoaded(const string &name)
name
const string&
Name of the extension to check.
return
bool
true if the extension is loaded, false otherwise.
Checks whether an extension is currently loaded. Example:
if (db.ExtensionIsLoaded("parquet")) {
    std::cout << "Parquet extension is loaded" << std::endl;
}

Static Methods

SourceID

static const char *SourceID()
return
const char*
Git commit hash of the DuckDB source code.
Returns the source code identifier (git hash) of the DuckDB build.

LibraryVersion

static const char *LibraryVersion()
return
const char*
Version string of the DuckDB library (e.g., “v1.0.0”).
Returns the version string of the DuckDB library. Example:
std::cout << "DuckDB version: " << DuckDB::LibraryVersion() << std::endl;

ReleaseCodename

static const char *ReleaseCodename()
return
const char*
Codename of the current DuckDB release.
Returns the codename of the current release.

StandardVectorSize

static idx_t StandardVectorSize()
return
idx_t
Standard vector size used by DuckDB (typically 2048).
Returns the standard vector size used internally by DuckDB for vectorized processing.

Platform

static string Platform()
return
string
Platform identifier string (e.g., “linux_amd64”).
Returns a string describing the platform DuckDB was built for.

Configuration Options

The DBConfig structure contains various configuration options:
DBConfig config;

// Access mode
config.options.access_mode = AccessMode::READ_ONLY;  // or READ_WRITE, AUTOMATIC

// Memory limit
config.options.maximum_memory = 1024 * 1024 * 1024;  // 1GB

// Thread count
config.options.maximum_threads = 4;

// Checkpoint threshold
config.options.checkpoint_wal_size = 16 * 1024 * 1024;  // 16MB

DuckDB db("database.db", &config);

Usage Examples

Basic Database Creation

#include "duckdb.hpp"

using namespace duckdb;

int main() {
    // In-memory database
    DuckDB db(nullptr);
    
    // Create a connection
    Connection con(db);
    con.Query("CREATE TABLE test(i INTEGER)");
}

Persistent Database with Configuration

DBConfig config;
config.options.access_mode = AccessMode::READ_WRITE;
config.options.maximum_threads = 8;

DuckDB db("analytics.db", &config);
Connection con(db);

auto result = con.Query("SELECT version()");
result->Print();

Multiple Connections

DuckDB db("shared.db");

// Multiple connections to the same database
Connection con1(db);
Connection con2(db);

con1.Query("CREATE TABLE data(x INTEGER)");
con2.Query("INSERT INTO data VALUES (42)");

auto result = con1.Query("SELECT * FROM data");
result->Print();  // Shows the data inserted via con2

Checking Version Information

DuckDB db(nullptr);

std::cout << "DuckDB Version: " << DuckDB::LibraryVersion() << std::endl;
std::cout << "Source ID: " << DuckDB::SourceID() << std::endl;
std::cout << "Platform: " << DuckDB::Platform() << std::endl;
std::cout << "Threads: " << db.NumberOfThreads() << std::endl;

See Also

Build docs developers (and LLMs) love