Storing a BLOB in a PostgreSQL Database using Python Last Updated : 21 Nov, 2022 Comments Improve Suggest changes 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 ayonssp Follow Improve Article Tags : Python Practice Tags : python Similar Reads PostgreSQL - Connecting to the Database using Python PostgreSQL in Python offers a robust solution for developers looking to interact with databases seamlessly. With the psycopg2 tutorial, we can easily connect Python to PostgreSQL, enabling us to perform various database operations efficiently. In this article, we will walk you through the essential 4 min read Handling PostgreSQL BLOB data in Python In this article, we will learn how to Handle PostgreSQL BLOB data in Python. BLOB is a Binary large object (BLOB) is a data type that can store any binary data.To store BLOB data in a PostgreSQL database, we need to use the Binary Large Object (BLOB) data type.By using the Binary Large Object (BLOB) 5 min read Save a image file on a Postgres database - Python In this article, we are going to see how to save image files on a postgresql database using Python. Psycopg2 is a driver, that is used, for interacting, with Postgres data, using the Python scripting language. It is, used to perform, CRUD operations on Postgres data. Data handled in applications c 4 min read Making a Flask app using a PostgreSQL database The Postgres database can be accessed via one of two methods in Python. Installing PgAdmin4 is the first step because it offers a user interface for interacting with databases and another for using the psycopg2 connector. In this post, we'll concentrate on a different approach that lets us alter the 4 min read Create a database in MongoDB using Python MongoDB is a general-purpose, document-based, distributed database built for modern application developers and the cloud. It is a document database, which means it stores data in JSON-like documents. This is an efficient way to think about data and is more expressive and powerful than the traditiona 2 min read PostgreSQL - Create table using Python Creating tables in a PostgreSQL database using Python is a common task for developers working with databases. This process involves defining the structure of your data and ensuring that your database is optimized for efficient storage and retrieval. In this article, we will walk through the steps of 3 min read CRUD Operations on Postgres using Async Database In Python CRUD stands for Create, Read, Update and Delete Operations. All these Operations can be made Asynchronous using the Async Database Connection. After making  Async Connection to Postgres Database, the performance of the Application improves significantly as all the operations are performed Concurrent 3 min read PostgreSQL Python - Querying Data Psycopg2 acts as a bridge between Python applications and PostgreSQL databases. Widely employed in diverse Python systems, from web applications to data analysis tools and other software projects, Psycopg2 enables developers to execute queries and manipulate data stored in PostgreSQL databases. In t 5 min read PostgreSQL - Connect To PostgreSQL Database Server in Python The psycopg database adapter is used to connect with PostgreSQL database server through python. Installing psycopg: First, use the following command line from the terminal: pip install psycopg If you have downloaded the source package into your computer, you can use the setup.py as follows: python s 4 min read PostgreSQL Python - Update Data in Table In this article, we are going to see how to update existing data in PostgreSQL tables using the pyscopg2 module in Python. In PostgreSQL, the UPDATE TABLE with where clause is used to update the data in the existing table from the database. Syntax: UPDATE <table_name> SET column1 = value1, c 2 min read Like