Skip to main content

Use PostgreSQL large objects for file storage

This storage is created to store important files that belong to a user. It should not be used to store publicly available files or videos. The main benefit is that it works with transaction and you can create or delete a file and be confident that the file will still exist if the transaction is rolled back.

When the storage saves a file, a new filename with the template .<original_extension> is always created and the attribute upload_to is not used.

You can use DbFileIO without a storage.

The settings:

  • PG_LO_STORAGE_DB_FOR_READ - a database for read files (default is default)
  • PG_LO_STORAGE_DB_FOR_WRITE - a database for create and write files (default is default)

Example

Add model:

from django.db import models
from django.db.models.signals import post_delete, post_init, post_save
from django.dispatch import receiver
from pg_lo_storage.fields import DbFileField
from pg_lo_storage.storage import db_file_storage, DbFileStorage

class Invoice(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    # data = DbFileField(storage=DbFileStorage("/invoice"), null=True, blank=True)
    data = models.FileField(storage=DbFileStorage("/invoice"), null=True, blank=True)

@receiver(post_init, sender=Invoice)
def user_file_initialized(sender, instance: Invoice, **kwargs):
    instance._lo_prev_state = {
        'data': instance.data.name if instance.data else None,
    }

@receiver(post_save, sender=Invoice)
def user_file_saved(sender, instance: Invoice, created: bool, **kwargs):
    # this is safe because large objects support transactions
    if not created and hasattr(instance, '_lo_prev_state'):
        state = instance._lo_prev_state
        if state.get('data'):
            instance.data.storage.delete(state['data'])

@receiver(post_delete, sender=Invoice)
def user_file_deleted(sender, instance: Invoice, **kwargs):
    # this is safe because large objects support transactions
    if instance.data:
        instance.data.storage.delete(instance.data.name)

Add function to serve files:

from django.http import HttpResponseForbidden
from pg_lo_storage.views import db_serve

@login_required
def invlice_serve(request: HttpRequest, filename: str) -> HttpResponse:
    if not Invoice.objects.filter(user=request.user, data=filename).exists():
        return HttpResponseForbidden()
    return db_serve(request, filename)

Work as a file:

from django.db import transaction
from pg_lo_storage.storage import DbFileIO

@transaction.atomic
def write(loid: int, data: bytes, pos: int) -> int:
    # DbFileIO work only with transactions
    with DbFileIO(loid, "wb") as file:
        file.seek(pos)
        file.write(data)
        return file.loid

Metadata

Release files for pg-lo-storage 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pg-lo-storage 0.2.2
File Size Uploaded Provenance Actions
pg_lo_storage-0.2.2.tar.gz 16.8 kB With provenance Details for pg_lo_storage-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pg-lo-storage 0.2.2
File Interpreter ABI Platform Actions
pg_lo_storage-0.2.2-py3-none-any.whl Python 3 none any Details for pg_lo_storage-0.2.2-py3-none-any.whl

Total release size: 25.4 kB

Release files / pg_lo_storage-0.2.2.tar.gz

Download URL pg_lo_storage-0.2.2.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
788d72800329723b166fc85bdcff639d07ebfc63d7b35f2e6505dbd20f49828d
BLAKE2b-256 checksum
How to use checksums
077e2e5be2839080f9e5e2c4ae112472638ddda916673ecd87af1b979ed6a4cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2025.

Transparency log

Release files / pg_lo_storage-0.2.2-py3-none-any.whl

Download URL pg_lo_storage-0.2.2-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e08c9172c6848beb3f9cd11ecf11527dddcc05f340772309723cc2f85b8d66c6
BLAKE2b-256 checksum
How to use checksums
6a1b7c7b4caaf1c29764dab36c476dae8c29c6e71cbc890b0ee600cdc7ff9504
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2025.

Transparency log

Security for pg-lo-storage 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pg_lo_storage-0.2.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page