# Using an in-memory sqlite database

**URL:** <https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472>\
**Category:** Deployment\
**Created:** [November 11, 2019, 2:12pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472 "2019-11-11T14:12:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrr](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chrr](https://forum.djangoproject.com/u/chrr)\
**Post date:** [November 11, 2019, 2:12pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/1 "2019-11-11T14:12:05Z")

</div>

Hi all,

in a recent project, using Django 2.2.7, the idea was to use an additional in-memory database, as in this configuration:

```auto
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'memory': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

```

In order to get the memory db initialized, we have this command in the config for one app called `memory_db_only`:

```auto
class MemoryDbOnlyConfig(AppConfig):
    name = 'memory_db_only'

    def ready(self):
        call_command('migrate',
                     app_label='memory_db_only',
                     verbosity=1,
                     interactive=False,
                     database='memory')

```

When starting a shell (`python manage.py shell`) and querying some model from the memory db, it works fine:

```auto
>>> from memory_db_only.models import MyModel
>>> MyModel.objects.using("memory").all()
<QuerySet []>

```

However, when we add a view to the `memory_db_only` app, like

```auto
def get_objects(request):
    obj = MyModel.objects.using("memory").all()
    return HttpResponse(str(obj))

```

an exception is raised:

```auto
OperationalError at /memory/objects
no such table: memory_db_only_mymodel

```

I would highly appreciate advice on how to setup Django in this particular scenario.  
Thanks  
Christoph

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [November 11, 2019, 5:38pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/2 "2019-11-11T17:38:09Z")

</div>

Did you migrate? You’d still need to do that.

---

<div class="post-metadata">

**Author:** ![Devil](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Devil](https://forum.djangoproject.com/u/Devil)\
**Post date:** [November 13, 2019, 4:28am UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/3 "2019-11-13T04:28:18Z")

</div>

Pardon my novice question, but really curious why would you use in-memory database won’t it be a bottleneck?

---

<div class="post-metadata">

**Author:** ![andyide](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andyide/32/5218_2.png) [@andyide](https://forum.djangoproject.com/u/andyide)\
**Post date:** [November 13, 2019, 9:44pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/4 "2019-11-13T21:44:51Z")

</div>

I guess if its a small project and speed is important you could use an in memory DB.

But I think you are right @Devil - I wouldn’t use it on anything with scale.

Having said that SQLite on a SSD is pretty fast, but maybe there are other reasons for using in memory SQLite

---

<div class="post-metadata">

**Author:** ![chrr](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chrr](https://forum.djangoproject.com/u/chrr)\
**Post date:** [November 14, 2019, 11:09am UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/5 "2019-11-14T11:09:39Z")

</div>

Update: Using `file::memory:` for the name of the memory database fixes the problem.

@ carltongibson: The migrations are run automatically whenever the server restarts, due to the changes to `MemoryDbOnlyConfig`

@ Devil: In our current project, we would like to use a relational cache, in order to store relational data from an external resource in a non-permanent manner.

@ andyide: Since we only use it for caching limited amounts of data, we don’t expect to see any scalability issues.

---

<div class="post-metadata">

**Author:** ![fahadanwaar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fahadanwaar/32/17049_2.png) [@fahadanwaar](https://forum.djangoproject.com/u/fahadanwaar)\
**Post date:** [November 13, 2023, 11:15am UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/6 "2023-11-13T11:15:43Z")

</div>

‘memory’: {  
‘ENGINE’: ‘django.db.backends.sqlite3’,  
‘NAME’: ‘file::memory:’,  
}

Hi I did that and it is still the same issue.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 13, 2023, 11:32am UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/7 "2023-11-13T11:32:53Z")

</div>

Ok, now _I’m_ confused.

Quoting directly from [the sqlite docs](https://www.sqlite.org/inmemorydb.html):

> When this is done, no disk file is opened. Instead, a new database is created purely in memory. The database ceases to exist as soon as the database connection is closed. Every :memory: database is distinct from every other. So, opening two database connections each with the filename “:memory:” will create two independent in-memory databases.

This gives me the impression that you would need to run migrate for every instance that opens a connection. Am I missing something here? (Also, I’m finding it difficult to understand what the value of this would be in a Django environment.)

---

<div class="post-metadata">

**Author:** ![fahadanwaar](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fahadanwaar/32/17049_2.png) [@fahadanwaar](https://forum.djangoproject.com/u/fahadanwaar)\
**Post date:** [November 13, 2023, 12:44pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/8 "2023-11-13T12:44:16Z")

</div>

@KenWhitesell  
file::memory:?cache=shared

We can use this. It would point to same cached on different connections.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 13, 2023, 1:09pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/9 "2023-11-13T13:09:06Z")

</div>

You’re still going to run into problems when you have more than one process or your process restarts.

From the same docs:

> Of course, all database connections sharing the in-memory database need to be in the same process. The database is automatically deleted and memory is reclaimed when the last connection to the database closes.

So each process instance of your project is going to be accessing a different database, and when the process is restarted, it all goes away.

---

<div class="post-metadata">

**Author:** ![andy912](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andy912/32/18418_2.png) [@andy912](https://forum.djangoproject.com/u/andy912)\
**Post date:** [January 15, 2024, 3:39pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/10 "2024-01-15T15:39:34Z")

</div>

Hi, I was wondering if someone else has had this issue, I haven’t been able to fix it with file::memory: or file::memory:?cache=shared as the db name. Is there a proper fix for this? The issue being that when running the server, no database tables are found because the migrations have not been applied.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 15, 2024, 3:46pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/11 "2024-01-15T15:46:46Z")

</div>

From refreshing my memory by rereading this thread, I have to ask - why are you trying to do this?

See the AppConfig class above for how you can run migrate on this database during the startup process.

---

<div class="post-metadata">

**Author:** ![andy912](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/andy912/32/18418_2.png) [@andy912](https://forum.djangoproject.com/u/andy912)\
**Post date:** [January 15, 2024, 5:16pm UTC](https://forum.djangoproject.com/t/using-an-in-memory-sqlite-database/472/12 "2024-01-15T17:16:28Z")

</div>

since we don’t really need a database (other than for some frameworks, as mentioned), we thought the easiest way to solve this was to use the in memory db. We can also use a file db, but I still wanted to make sure if in-memory was still possible.
