Skip to main content

DuckDB

A DuckDB database file is SereneDB's native attachable database. ATTACH opens a DuckDB file for reading and writing, and the alias is inferred from the file name unless an explicit one is given. For the general ATTACH and DETACH syntax, see ATTACH AND DETACH.

Attach the database file.db with the alias inferred from the name (file):

Query
ATTACH 'file.db';

Attach the database file.db with an explicit alias (file_db):

Query
ATTACH 'file.db' AS file_db;

Attach the database file.db only if the inferred alias file does not yet exist:

Query
ATTACH IF NOT EXISTS 'file.db';

Attach the database file.db only if the explicit alias file_db does not yet exist:

Query
ATTACH IF NOT EXISTS 'file.db' AS file_db;

Attach the database file2.db as alias file_db, detaching and replacing the existing alias if it exists:

Query
ATTACH OR REPLACE 'file2.db' AS file_db;

Create a table in the attached database with alias file:

Query
CREATE TABLE db16.new_table (i INTEGER);

Detach the database with alias file:

Query
DETACH db16;

Show a list of all attached databases:

Query
SHOW DATABASES;

Change the default database that is used to the database file:

Query
USE db16;

Options

Zero or more options may be provided within parentheses following the ATTACH statement. Parameter values can be passed in with or without wrapping in single quotes. Arbitrary expressions may be used for parameter values.

NameDescriptionTypeDefault value
ACCESS_MODEAccess mode of the database (AUTOMATIC, READ_ONLY, or READ_WRITE).VARCHARautomatic
COMPRESSWhether the database is compressed. Only applicable for in-memory databases.VARCHARfalse
TYPEThe database type. DUCKDB for a file, or postgres deduced from a connection string.VARCHARDUCKDB
BLOCK_SIZEThe block size of a new database file. Must be a power of two and within [16384, 262144]. Cannot be set for existing files.UBIGINT262144
ROW_GROUP_SIZEThe row group size of a new database file.UBIGINT122880
STORAGE_VERSIONThe version of the storage used.VARCHARv1.0.0
ENCRYPTION_KEYThe encryption key used for encrypting the database.VARCHAR-
ENCRYPTION_CIPHERThe encryption cipher used for encrypting the database (CTR or GCM).VARCHAR-
RECOVERY_MODERecovery mode for the database. no_wal_writes disables WAL writes, improving performance at the cost of crash recovery.VARCHAR-

Attach the database file.db in read only mode:

Query
ATTACH 'file.db' (READ_ONLY);

Attach the database file.db with a block size of 16 kB:

Query
ATTACH 'file.db' (BLOCK_SIZE 16_384);

Attach the database file.db with a row group size of 2048 rows:

Query
ATTACH 'file.db' (ROW_GROUP_SIZE 2048);

Attach the database file.db with WAL writes disabled for improved performance:

Query
ATTACH 'file.db' (RECOVERY_MODE no_wal_writes);

Explicit Storage Versions

SereneDB allows explicitly specifying the storage version. Using this, you can opt-in to newer forwards-incompatible features:

Query
ATTACH 'file.db' (STORAGE_VERSION 'v1.2.0');

This setting specifies the minimum SereneDB version that should be able to read the database file. When database files are written with this option, the resulting files cannot be opened by older SereneDB versions than the specified version. They can be read by the specified version and all newer versions of SereneDB.

Database Encryption

SereneDB supports database encryption. By default, it uses AES encryption with a key length of 256 bits using the recommended GCM mode. The encryption covers the main database file, the write-ahead-log (WAL) file and even temporary files. To attach to an encrypted database, use the ATTACH statement with an ENCRYPTION_KEY:

Query
ATTACH 'encrypted.db' AS enc_db (ENCRYPTION_KEY 'secret_key');

To change the AES mode to CTR, use the ENCRYPTION_CIPHER option:

Query
ATTACH 'encrypted.db' AS enc_db (ENCRYPTION_KEY 'secret_key', ENCRYPTION_CIPHER 'CTR');

Remote files (HTTP / S3)

ATTACH supports HTTP and S3 endpoints. For these, it creates a read-only connection by default. Therefore, the following two commands are equivalent:

Query
ATTACH 'https://blobs.duckdb.org/databases/stations.duckdb' AS stations_db;
ATTACH 'https://blobs.duckdb.org/databases/stations.duckdb' AS stations_db (READ_ONLY);

Similarly, the following two commands connecting to S3 are equivalent:

Query
ATTACH 's3://⟨blobs-duckdb⟩/databases/stations.duckdb' AS stations_db;
ATTACH 's3://⟨blobs-duckdb⟩/databases/stations.duckdb' AS stations_db (READ_ONLY);