Working with Databases — Data Model


    Description — Working with Couchbase Lite Databases
    Related Content — Blobs | Documents | Indexing |

    New Database

    As the top-level entity in the API, new databases can be created using the Database class by passing in a name, configuration, or both. The following example creates a database using the Database(string name) method.

    var db = new Database("my-database");

    Just as before, the database will be created in a default location. Alternatively, the Database(string name, DatabaseConfiguration config) initializer can be used to provide specific options in the DatabaseConfiguration object such as the database directory.

    Database Encryption

    This is an Enterprise Edition feature.

    Couchbase Lite on C#.Net includes the ability to encrypt Couchbase Lite databases. This allows mobile applications to secure the data at rest, when it is being stored on the device. The algorithm used to encrypt the database is 256-bit AES.

    To enable encryption, you must set the DatabaseConfiguration.encryptionKey property to the encryption key of your choice. Provide this encryption key every time the database is opened.

    // Create a new, or open an existing database with encryption enabled
    var config = new DatabaseConfiguration
        // Or, derive a key yourself and pass a byte array of the proper size
        EncryptionKey = new EncryptionKey("password")
    using (var db = new Database("seekrit", config)) {
        // Change the encryption key (or add encryption if the DB is unencrypted)
        db.ChangeEncryptionKey(new EncryptionKey("betterpassw0rd"));
        // Remove encryption

    Couchbase Lite does not persist the key. It is the application’s responsibility to manage the key and store it in a platform specific secure store such as Apple’s Keychain or Android’s Keystore.

    An encrypted database can only be opened with the same language SDK that was used to encrypt it in the first place (Swift, C#, Java, Java (Android) or Objective-C). For example, if a database is encrypted with the Swift SDK and then exported, it will only be readable with the Swift SDK.

    Upgrading from 1.x when Encryption is Enabled

    If you’re migrating an application from Couchbase Lite 1.x to 2.x, note that the automatic database upgrade functionality is not supported for encrypted databases. Thus, to upgrade an encrypted 1.x database, you should do the following:

    1. Disable encryption using the Couchbase Lite 1.x framework (see 1.x encryption guide)

    2. Open the database file with encryption enabled using the Couchbase Lite 2.x framework.

    Since it is not possible to package Couchbase Lite 1.x and Couchbase Lite 2.x in the same application this upgrade path would require two successive upgrades. If you are using Sync Gateway to synchronize the database content, it may be preferable to run a pull replication from a new 2.x database with encryption enabled and delete the 1.x local database.

    Finding a Database File

    Where a database goes by default depends on the platform it is running on. Here are the defaults for each platform:

    • .NET Core: Path.Combine(AppContext.BaseDirectory, "CouchbaseLite") (unless the app context is altered [e.g. by XUnit], this will be the same directory as the output binary)

    • UWP: Windows.Storage.ApplicationData.Current.LocalFolder.Path (Inside the installed app sandbox. Note that this sandbox gets deleted sometimes when debugging from inside Visual Studio when the app is shutdown)

    • Xamarin iOS: In a folder named CouchbaseLite inside of ApplicationSupportDirectory (this can be retrieved more easily from the simulator using the SimPholders utility)

    • Xamarin csharp: Using the Context passed in the Activate() method, Context.FilesDir.AbsolutePath (database can be retrieved using adb)

    CLI tool

    cblite is a command-line tool for inspecting and querying Couchbase Lite 2.x databases.

    You can download and build it from the couchbaselabs GitHub repository. Note that the cblite tool is not supported by the Couchbase Support Policy.


    If you are using a Couchbase Lite release prior to 2.5 see Deprecated functionality

    Unresolved include directive in modules/csharp/pages/learn/csharp-database.adoc - include::partial$logging.adoc[]

    Logging functionality prior to Release 2.5

    The pre-Couchbase Lite 2.5 logging functionality is deprecated. It was superseded by an enhanced Logging API in that release. This information is included for completeness only.

    The log messages are split into different domains (LogDomains) which can be tuned to different log levels. The following example enables verbose logging for the replicator and query domains.

    Example 1. Enable verbose logging on a domain
    Database.SetLogLevel(LogDomain.Replicator, LogLevel.Verbose);
    Database.SetLogLevel(LogDomain.Query, LogLevel.Verbose);
    Database.SetLogLevel(LogDomain.Replicator, LogLevel.Verbose);
    Database.SetLogLevel(LogDomain.Query, LogLevel.Verbose);