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

~2 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

Related Articles

Django Tasks Framework: A Complete Guide to Background Task Execution in Django 6.0

Django 6.0 introduces the Tasks framework, a built‑in system for defining and queuing background work outside the request–response cycle. This article explains how Tasks work, how to configure backends, how to define and enqueue tasks, how context works, and how to integrate third‑party worker systems for production environments.

Continue

Asynchronous Support in Django: A Complete Guide to Async Views, ORM, Middleware, Performance, and Safety

This article explains Django’s asynchronous (async) capabilities, including async views, ASGI support, middleware behavior, async ORM features, performance considerations, handling disconnects, and Django’s async safety protections. It also covers how to use sync_to_async(), async ORM methods, and how to safely run synchronous code in async environments.

Continue

Django System Check Framework: A Complete Guide to Writing, Registering, Running, and Testing System Checks

This article explains Django’s System Check Framework—a powerful mechanism for detecting configuration issues, validating project structure, and ensuring code quality. It covers how checks are executed, how to write custom checks, how messages work, how to register and tag checks, how to extend checks for fields and models, and how to write both unit and integration tests for system checks.

Continue

Django Signals: A Complete Guide to Listening, Connecting, Sending, and Managing Application Events

This article provides a comprehensive explanation of Django’s signal system—an event‑driven mechanism that allows decoupled applications to react to actions occurring elsewhere in the framework. It covers how to define receivers, connect signals, use decorators, handle specific senders, organize signal code, and follow best practices to avoid complexity.

Continue

Understanding Django Settings: Configuration, Environment Management, and Best Practices

This article provides a complete overview of Django’s settings system. It explains how settings files work, how to designate a settings module, how to use settings in your code, how to configure Django manually, how to secure sensitive settings, and how to work with custom default settings. It also covers the role of django.setup() for standalone scripts.

Continue

Serializing and Deserializing Django Objects: A Complete Guide to Django’s Serialization Framework

This article explains Django’s serialization framework, including how to serialize and deserialize model instances, work with subsets of fields, handle inherited models, use different serialization formats (JSON, XML, YAML, JSONL), and understand how relational fields are represented. It also covers DeserializedObject behavior and common pitfalls.

Continue