Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql in memory database django

For performance issues I would like to execute an optimization algorithm on an in memory database in django (I'm likely to execute a lot of queries). I know it's possible to use a sqlite in memory (How to run Django's test database only in memory?) but I would rather use postgresql because our prod database is a postgresql one.

Does someone knows how to tell django to create the postgresql database in the memory ?

Thanks in advance

like image 805
Svan Avatar asked May 13 '16 18:05

Svan


People also ask

Is PostgreSQL an in memory database?

Tapping into RAM with shared and OS cachesPostgres is a disk-based database, and it's important to tap into RAM even if your entire architecture is designed around disk access. This can reduce the latency from days to minutes if you judge by the latency at human scale (Figure1).

Can Postgres run in memory?

You can't run Pg in-process, in-memory PostgreSQL is implemented in C and compiled to platform code. Unlike H2 or Derby you can't just load the jar and fire it up as a throwaway in-memory DB. Unlike SQLite, which is also written in C and compiled to platform code, PostgreSQL can't be loaded in-process either.

Is PostgreSQL good for Django?

If you are building an application with maps or you are storing geographical data, you need to use PostgreSQL, as GeoDjango is only fully compatible with PostgreSQL. PostgreSQL has the richest set of features that are supported by Django.


2 Answers

This is premature optimization. Postgresql is very very fast even if you are running it on a cold metal hard disk provided you use the right indexes. If you don't persist the data on disk, you are opening yourself upto a world of pain.

If on the other hand you want to speed up your tests by running an in memory postgresql database you can try something like these non durability optimizations:

http://www.postgresql.org/docs/9.1/static/non-durability.html

The most drastic suggestion is to use a ramdisk on that page. Here's how to set up one. After following the OS/Postgresql steps edit django settings.py and add the tablespace to the DATABASES section.

Last but not least: This is just a complete waste of time.

like image 126
e4c5 Avatar answered Oct 18 '22 11:10

e4c5


This is not possible. You cannot use PostgreSQL exclusively in memory; at least not without defeating the purpose of using PostgreSQL over something else. An in-memory data store like Redis running alongside PostgreSQL is the best you can do.

Also, at this point, the configuration is far out of Django's hands. It will have to be done outside of Django's environment.

It appears you may be missing some understanding about how these systems work. Build your app first to verify that everything is working, then worry about optimizing the database in such ways.

like image 41
Jamie Counsell Avatar answered Oct 18 '22 13:10

Jamie Counsell