Using Tablespaces in Django: Table Storage, Index Placement, and Database Support

This article explains how Django supports tablespaces for organizing database storage. It covers how to declare tablespaces for tables and indexes, how DEFAULT_TABLESPACE and DEFAULT_INDEX_TABLESPACE influence behavior, and how tablespaces behave across different database backends. An example model demonstrates how table and index placement works in practice.

tablespace, db_tablespaceDEFAULT_TABLESPACE, DEFAULT_INDEX_TABLESPACEindex tablespace, Django ORM, PostgreSQL, Oracle

~3 min read • Updated Mar 10, 2026

1. Introduction to Tablespaces

Tablespaces are a common database optimization technique used to control how data is physically stored on disk. They allow you to place tables and indexes in different storage locations for performance or organizational reasons.

Important: Django does not create tablespaces for you. You must create and manage them directly in your database system.

2. Declaring Tablespaces for Tables

You can specify a tablespace for a model’s database table using the db_tablespace option inside class Meta:


class MyModel(models.Model):
    ...
    class Meta:
        db_tablespace = "my_tablespace"

This also applies to automatically generated many‑to‑many tables.

You can set a global default using:


DEFAULT_TABLESPACE = "default_ts"

This is useful for built‑in Django apps or third‑party apps you cannot modify.

3. Declaring Tablespaces for Indexes

You can specify a tablespace for indexes in two ways:

3.1 Using Index()


models.Index(fields=["shortcut"], db_tablespace="other_indexes")

3.2 Using Field(db_tablespace=...)

For single‑column indexes:


name = models.CharField(max_length=30, db_index=True, db_tablespace="indexes")

If the field does not have an index, the option is ignored.

You can set a global default for index tablespaces:


DEFAULT_INDEX_TABLESPACE = "default_index_ts"

If neither db_tablespace nor DEFAULT_INDEX_TABLESPACE is set, the index is created in the same tablespace as the table.

4. Full Example


class TablespaceExample(models.Model):
    name = models.CharField(max_length=30, db_index=True, db_tablespace="indexes")
    data = models.CharField(max_length=255, db_index=True)
    shortcut = models.CharField(max_length=7)
    edges = models.ManyToManyField(to="self", db_tablespace="indexes")

    class Meta:
        db_tablespace = "tables"
        indexes = [
            models.Index(fields=["shortcut"], db_tablespace="other_indexes")
        ]

Explanation:

  • The model table and M2M table → stored in tables tablespace.
  • Index for name → stored in indexes tablespace.
  • Index for data → no tablespace specified → stored in tables (default).
  • Index for shortcut → stored in other_indexes.
  • M2M indexes → stored in indexes.

5. Database Support

Tablespace support varies by backend:

DatabaseSupports Tablespaces?
PostgreSQL✔ Yes
Oracle✔ Yes
SQLite✘ No
MariaDB✘ No
MySQL✘ No

If the backend does not support tablespaces, Django silently ignores all tablespace‑related options.

Conclusion

Tablespaces provide fine‑grained control over where tables and indexes are stored. Django exposes this functionality through db_tablespace options on models, fields, and indexes, while leaving the actual creation and management of tablespaces to the database administrator. Understanding backend support is essential for using tablespaces effectively.

Written & researched by Dr. Shahin Siami