juliaduckdb

In Julia, how to connect to DuckDB in read-only mode?


DuckDB has a read-only access mode that allows multiple processes to read from the same database file, according to another SO answer and the documentation.

How can I connect to my database (file my.duckdb) in read-only mode? I cannot find that information in the documentation. After reading some source code of DuckDB.jl, I guessed the following

cnf = DuckDB.Config()
DuckDB.set_config(cnf, "access_mode", "READ_ONLY")
conn = DBInterface(DuckDB, "my.duckdb", cnf)

This works if I run it in one Julia REPL, but running it in a second REPL while the other REPL still has conn open gives me an error message.


Solution

  • The following code works for me in two separate REPLs. It is slightly different from your code. Not sure if the code you posted differs slightly from what you ran because the last line of what you posted doesn't run at all for me.

    julia> using DuckDB
    
    julia> cnf = DuckDB.Config()
    DuckDB.Config(Ptr{Nothing} @0x000000010600bc00)
    
    julia> DuckDB.set_config(cnf, "access_mode", "READ_ONLY")
    
    julia> conn = DBInterface.connect(DuckDB.DB, "my.duckdb", cnf)
    DuckDB.DB("my.duckdb")