# How do I properly close a DuckDb database?

**URL:** https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598
**Category:** Data
**Tags:** database, duckdb
**Created:** [June 20, 2023, 11:01am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598 "2023-06-20T11:01:25Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)
#### Post date: [June 20, 2023, 11:01am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/1 "2023-06-20T11:01:25Z")

</div>

I cannot run the following code twice in one julia session:

```julia
using DuckDB 
using DBInterface 
db = DuckDB.DB("C:\\Temp\\test.duckdb")
t = DBInterface.execute(db, "SELECT 1") 
DuckDB.close_database(db)

```

On the second run I get the error:

```julia
`ERROR: IO Error: Cannot open file "C:\Temp\test.duckdb": The process cannot access the file because it is being used by another process.`

```

I have tried

```julia
DBInterface.close!(db)

```

in place of

```julia
DuckDB.close_database(db)

```

it makes no difference.

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [June 20, 2023, 11:37am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/2 "2023-06-20T11:37:31Z")

</div>

DuckDB defines `close(db::DuckDB.DB)` function so `close(db)` would be enough.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [June 20, 2023, 1:06pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/3 "2023-06-20T13:06:26Z")

</div>

This does not error for me:

```julia
using DuckDB, DBInterface
db = DBInterface.connect(DuckDB.DB,"C:\\Temp\\test.duckdb")
t = DBInterface.execute(db, "SELECT 1")
DBInterface.close!(db)

db = DBInterface.connect(DuckDB.DB,"C:\\Temp\\test.duckdb")
t = DBInterface.execute(db, "SELECT 1")
DBInterface.close!(db)

```

This is how I create and close connections, never had issues (except sometimes within Pluto, but I think that is separate).

---

<div class="post-metadata">

### Author: ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)
#### Post date: [June 22, 2023, 3:55am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/4 "2023-06-22T03:55:39Z")

</div>

Just using `close(db)` gives the same error

---

<div class="post-metadata">

### Author: ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)
#### Post date: [June 22, 2023, 3:59am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/5 "2023-06-22T03:59:40Z")

</div>

Still the same problem with your approach:

```julia
ERROR: IO Error: Cannot open file "C:\Temp\test.duckdb": The process cannot access the file because it is being used by another process.

Stacktrace:
 [1] DuckDB.DuckDBHandle(f::String, config::DuckDB.Config)
   @ DuckDB C:\Users\kobus\.julia\packages\DuckDB\VdbjW\src\database.jl:17
 [2] DuckDB.DB(f::String, config::DuckDB.Config)
   @ DuckDB C:\Users\kobus\.julia\packages\DuckDB\VdbjW\src\database.jl:82
 [3] DB
   @ C:\Users\kobus\.julia\packages\DuckDB\VdbjW\src\database.jl:90 [inlined]
 [4] connect(#unused#::Type{DuckDB.DB}, f::String)
   @ DuckDB C:\Users\kobus\.julia\packages\DuckDB\VdbjW\src\database.jl:105
 [5] top-level scope
   @ c:\Source\Repos\RDAIngest.jl\src\close_test.jl:7

```

System: Windows 11  
Latest version of Visual Studio Code  
Julia v1.9.1

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [June 22, 2023, 11:23am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/6 "2023-06-22T11:23:05Z")

</div>

What version of duckdb?

---

<div class="post-metadata">

### Author: ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)
#### Post date: [June 22, 2023, 12:54pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/7 "2023-06-22T12:54:21Z")

</div>

Apologies,  
DuckDB v0.8.0

Updated to DuckDB v0.8.1 - same story

---

<div class="post-metadata">

### Author: ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)
#### Post date: [June 22, 2023, 1:40pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/8 "2023-06-22T13:40:35Z")

</div>

Maybe the problem is with windows not “letting go” of the file.

I’ve seen permissions change in new files that prevented successive  
access by the same program or user (in a shell session).

What about trying to use separate filenames for each instance,  
maybe with `Base.Filesystem.mktemp` or some such?

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [June 22, 2023, 1:56pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/9 "2023-06-22T13:56:07Z")

</div>

Very strange because it sounds like your system matches mine in this regard (latest Julia & DuckDB & Win 11).

---

<div class="post-metadata">

### Author: ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)
#### Post date: [June 23, 2023, 4:33am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/10 "2023-06-23T04:33:14Z")

</div>

I have done some further tests and I get the same error if I run it under Windows Server 2019 and Windows 10. Same Julia 1.9.1. In these cases I also used a different user account to my original test.

I also ran the test in a Julia environment with just DBInterface and DuckDB installed. Same problem.

Any thoughts on where I could look next?

I would very much like to consider DuckDB for a project I have, but not being able to reliably close and open the database is a problem.

---

<div class="post-metadata">

### Author: ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)
#### Post date: [June 23, 2023, 4:37am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/11 "2023-06-23T04:37:28Z")

</div>

Unfortunately that won’t work for me, I need repeated access to the same database in a single julia session.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [June 23, 2023, 9:15am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/12 "2023-06-23T09:15:10Z")

</div>

Have you tried the duckdb cli to see if you also get errors with that?

Have you tried with a different file location?

---

<div class="post-metadata">

### Author: ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)
#### Post date: [June 23, 2023, 10:15am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/13 "2023-06-23T10:15:50Z")

</div>

The DBeaver application has no problem with opening and closing DuckDb databases, including DuckDb databases created via julia. I have not worked with the DuckDb CLI. I have tried various file locations - it makes no difference.

I have noticed that when I close the DuckDb database in julia the temporary `*.wal` file that gets created when a database is opened, is not deleted on database closure, whereas when a database opened and closed with an application like DBeaver, the `*wal` file is removed.

I can successfully open the dummy database `test.duckdb` created by the julia code and upon closing the connection in DBeaver, the `*.wal` file is removed.

If I try to open the database using DBeaver, whilst the julia process is still active (after executing the first set of database open and close statements), I get the same error in DBeaver:

```julia
Reason:
IO Error: Cannot open file "d:\temp\test.duckdb": The process cannot access the file because it is being used by another process.

```

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [June 23, 2023, 12:28pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/14 "2023-06-23T12:28:20Z")

</div>

Hmmm ok, amending my first reply, I do get the bug. But it seems like mine happens one step after yours?

If I just simply take this and paste it into the REPL one time, no issues.

```julia
using DuckDB, DBInterface
db = DBInterface.connect(DuckDB.DB,"C:\\Temp\\test.duckdb")
t = DBInterface.execute(db, "SELECT 1")
DBInterface.close!(db)

db = DBInterface.connect(DuckDB.DB,"C:\\Temp\\test.duckdb")
t = DBInterface.execute(db, "SELECT 1")
DBInterface.close!(db)

```

If I try to run it as a script – errors.  
If I paste it twice in the same REPL session – errors.

I was going to say that you should open an issue but I see you already have.

What use case do you need to repeatedly open and close the db in the same session?

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [June 23, 2023, 12:50pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/15 "2023-06-23T12:50:42Z")

</div>

Can confirm that the error doesn’t happen when using python-duckdb via PythonCall.jl so the issue is Julia client specific.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [June 23, 2023, 1:18pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/16 "2023-06-23T13:18:30Z")

</div>

The error happens because they never get closed 😉

I will update your issue on the repo. Should be a simple fix.

---

<div class="post-metadata">

### Author: ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)
#### Post date: [June 23, 2023, 1:25pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/17 "2023-06-23T13:25:07Z")

</div>

I have a package that imports different datasets from different sources into the database (production system actually uses Sqlite). The function to import a specific data source open the database and close the database on import completion. The different functions a called from a main script one after another.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [June 23, 2023, 1:31pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/18 "2023-06-23T13:31:27Z")

</div>

This fix plus @aplavin awesome PR to make DuckDB Tables.jl compatible (hopefully to be merged soon!) are two big steps forward for Julia+DuckDB users.

---

<div class="post-metadata">

### Author: ![AUK1939](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@AUK1939](https://discourse.julialang.org/u/AUK1939)
#### Post date: [February 20, 2025, 8:03pm UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/19 "2025-02-20T20:03:37Z")

</div>

The error still persists for me. The workaround was

```
using DuckDB, DBInterface
db = DuckDB.open("C:\\Temp\\test.duckdb")
conn = DuckDB.connect(db)
t = DBInterface.execute(conn, "SELECT 1")
DBInterface.close!(conn)
DuckDB.close(db)

db = DuckDB.open("C:\\Temp\\test.duckdb")
conn = DuckDB.connect(db)
t = DBInterface.execute(conn, "SELECT 1")
DBInterface.close!(conn)
DuckDB.close(db)

```

---

<div class="post-metadata">

### Author: ![drizk1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drizk1/32/208422_2.png) [@drizk1](https://discourse.julialang.org/u/drizk1)
#### Post date: [February 21, 2025, 12:33am UTC](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598/20 "2025-02-21T00:33:19Z")

</div>

There is some discussion over here which may be useful to help with this issue  
[https://discourse.julialang.org/t/is-it-hard-to-support-julia-udfs-in-duckdb/118509/38?u](https://discourse.julialang.org/t/is-it-hard-to-support-julia-udfs-in-duckdb/118509/38?u)

[Next page](https://discourse.julialang.org/t/how-do-i-properly-close-a-duckdb-database/100598.md?page=2)
