Django PostgreSQL File Upload & Extract

Featured Image
Hasan-Uz-Zaman Ashik

Written by Hasan-Uz-Zaman

October 3, 2022

Storing files in a relational database like PostgreSQL is not a recommended approach, as databases are optimized for structured data, not unstructured data like files. There are several reasons why it’s not ideal to store files in a database:

Performance: Querying and retrieving large files from a database can be slow and resource-intensive.

Scalability: As the amount of stored data grows, the database performance can degrade, which can impact the overall system performance.

Limitations on file size: Most relational databases have limits on the size of data that can be stored in a single field. If you need to store large files, you may run into size limitations.

Increased complexity: Managing files within a database adds an additional layer of complexity to your system, making it more difficult to maintain and troubleshoot.

Instead of storing files in a database, it’s recommended to use a file storage system that is optimized for handling unstructured data, such as a cloud storage service or a local file system. You can then store a reference to the file location in the database and use that reference to retrieve the file when necessary.

In any case, this post will outline how to upload a file, store the encoded data in a PostgreSQL database, and then extract that data to a Django web page if you still wish to keep files in PostgreSQL.

Steps:

  1. Create database table with column type bytea
  2. Create Django html form to upload file.
  3. Encode file to base64 and insert to PostgreSQL database with file extension.
  4. Retrieve encoded data from database, decode it, and pass to html page.
  5. Finally, Show file in Django HTML page.

For better view scripts can be downloaded from here.

Creating database table:

A table is created for storing two files with their extension names. User will upload the file and select the file type from a Django html form.

 

Create Django html form to upload file:

In setting.py of Django project app a media directory location is defined where file will be uploaded initially.

 

 

 

HTML page showing form:

 

This HTML form will submit files to our defined media directory. 

In view.py we will capture the values submitted from html form. We are encoding file to base64, and inserting to PostgreSQL database with corresponding file extension names.

 

Retrieve encoded data from database, decode it, and pass to html page:

To recover data we need to decode it after selecting it from database. We will covert it to bytes tobytes(), and decode to UTF-8 (decode(“utf-8”)).

 

 

Database showing that both image and pdf files are stored properly.

Finally, Show file in Django html page.

 

Html page showing decoded file data

 

 

You May Also Like…

Hasan-Uz-Zaman

Hasan-Uz-Zaman

Network Engineer

Zaman is an aspiring Technical Writer and passionate about software-defined networking (SDN), Network Automation, Ansible, Log data management (Syslog-ng), Python tools, Web Application development (django) etc.

Let's start talking!

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *