How to Retrieve Blob Datatype from Postgres with Python Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report In this article, We will learn How to retrieve BLOB from a PostgreSQL database. BLOB is a Binary large object (BLOB) is a data type that can store any binary data.To Retrieve Blob Datatype from Postgres with Python we will use psycopg2.Stepwise Implementation:Connect to the PostgreSQL server.Create a cursor with the help of cursor() method in Python. Execute the Retrieve Query using the execute() method with BLOB VALUES. And then Close the Cursor and commit the changes. The below code is an example to Retrieve BLOB data in a PostgreSQL database. Python3 import psycopg2 from config import config # connect to the PostgreSQL server # & creating a cursor object conn = psycopg2.connect(**config) cur = conn.cursor() # Retrieve BLOB data from the database. cur.execute('SELECT * FROM BLOB_DataStore') db = cur.fetchall() BLOB = db[0][2] open("FromDB"+db[0][1], 'wb').write(BLOB) cur.close() conn.commit() Complete Function to Retrieve the BLOB data into the database The code to Retrieve BLOB data in a PostgreSQL database with the Table name blob_datastore. Retrieve Blob Datatype from Postgres Python3 # Complete Function to Retrieve # the BLOB data into the database. import psycopg2 from config import config # This Function will Creates File from binary data. def Binary_To_File(BLOB, FileName, oldFileName): with open(f"{FileName}", 'wb') as file: file.write(BLOB) print(f"{oldFileName} File saved With Name name {FileName}") def retrieve_BLOB(S_No, newFileName): """ Retrieve a BLOB From 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() # Retrieve BLOB data from the database. cur.execute('SELECT * FROM BLOB_DataStore') db = cur.fetchall() BLOB = db[S_No-1][2] # open("FromDB"+db[0][1], 'wb').write(BLOB) Binary_To_File(BLOB, newFileName, db[S_No-1][1]) # 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() retrieve_BLOB(1, 'OctaFromDB.jpg') Output: Retrieve Blob Datatype from PostgresRetrieving Different Types of Files(BLOB Datatype) The code to Retrieve BLOB data from PostgreSQL database With the Table name blob_datastore. The type of data that we will Retrieve: MP4PDFDOCSImageVideogifHTMLMP3Retrieve Blob Datatype from Postgres Example: 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 fetch data from the database. cur.execute('SELECT * FROM BLOB_DataStore') # open(file,'wb').write() is used to # write the binary data to the file. for row in cur.fetchall(): BLOB = row[2] open("new"+row[1], 'wb').write(BLOB) print(row[0], row[1], "BLOB Data is saved\ in Current Directory") # 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() Output: Retrieve Blob Datatype from Postgres Create Quiz Comment A ayonssp Follow 1 Improve A ayonssp Follow 1 Improve Article Tags : Python Python PostgreSQL Python Pyscopg2 Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like