Storing a BLOB in a PostgreSQL Database using Python Last Updated : 21 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report This article focuses on, Storing BLOB in a PostgreSQL database. BLOB is a Binary large object (BLOB) is a data type that can store any binary data.To Store Blob data in a Postgres database Table, we will use psycopg2.The table for storing BLOB data in PostgreSQL is called a Large Object table and the data type is byte.We can store png, jpg, gif, pdf, CSV, mp3 & mp4 files.Stepwise Implementation:Connect to the PostgreSQL server and to Connect with PostgreSQL Database we use connect() function. Create a cursor with the help of the cursor() method. Execute the Insert Query using the execute() method with BLOB VALUES.To store any binary data in a PostgreSQL database First, we need to convert the File to Binary Large Object (BLOB) data type.Close the Cursor and commit the changes. The below code is an example to store BLOB data in a PostgreSQL database. Where the table name is blob_datastore. Python3 import psycopg2 from config import config # This Function will open & # convert the image or file data # to binary data. def convert_To_Binary(filename): with open(filename, 'rb') as file: data = file.read() return data def insert_BLOB(S_No, FileName): """ insert a BLOB into a table """ conn = None try: # connect to the PostgreSQL server # & creating a cursor object conn = psycopg2.connect(**config) # Creating a cursor with name cur. cur = conn.cursor() # Binary Data file_data = convert_To_Binary(FileName) # BLOB DataType BLOB = psycopg2.Binary(file_data) # SQL query to insert data into the database. cur.execute( "INSERT INTO blob_datastore(s_no,file_name,blob_data)\ VALUES(%s,%s,%s)", (S_No, FileName, BLOB)) # Close the connection cur.close() except(Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: # Commit the changes to the database conn.commit() # Driver's Code # Let's Run the insert_BLOB Function insert_BLOB(1, 'newOcta.jpg') Output: Storing a BLOB in a PostgreSQL DatabaseStoring Different Types of Files(BLOB Datatype) Hence we have established a connection with the PostgreSQL database and stored different types of File's in the PostgreSQL database. In this example, we will store a video file and a pdf in database. Python3 import psycopg2 from config import config conn = None try: # connect to the PostgreSQL server conn = psycopg2.connect(**config) # Creating a cursor with name cur. cur = conn.cursor() # SQL query to insert data into the database. # open('File,'rb').read() is used to read the file. # where open(File,'rb').read() will return # the binary data of the file. # psycopg2.Binary(File_in_Bytes) is used to # convert the binary data to a BLOB data type. BLOB_vdo = psycopg2.Binary( open('files\cartoon.mp4', 'rb').read()) BLOB_pdf = psycopg2.Binary( open('files\BlobNotes.pdf', 'rb').read()) cur.execute('INSERT INTO blob_datastore(s_no,file_name,\ blob_data) VALUES (%s,%s,%s);', (1, 'cartoon.mp4', BLOB_vdo)) cur.execute('INSERT INTO blob_datastore(s_no,file_name,\ blob_data) VALUES (%s,%s,%s);', (2, 'BlobNotes.pdf', BLOB_pdf)) # close the cursor cur.close() except(Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: # Commit the changes to the database conn.commit() Output: Storing a BLOB in a PostgreSQL Database Comment More infoAdvertise with us Next Article Storing a BLOB in a PostgreSQL Database using Python A ayonssp Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like