How to Retrieve Blob Datatype from Postgres with Python Last Updated : 21 Nov, 2022 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us Next Article How to Retrieve Blob Datatype from Postgres with Python ayonssp Follow Improve Article Tags : Python Python PostgreSQL Python Pyscopg2 Practice Tags : python Similar Reads How To Connect and run SQL Queries to a PostgreSQL Database from Python In this PostgreSQL Python tutorial, we will explain how to connect to a PostgreSQL database using Python and execute SQL queries. Using the powerful psycopg2 library, we can seamlessly interact with our PostgreSQL database from Python, making it easy to perform tasks like inserting, updating, and re 4 min read Storing a BLOB in a PostgreSQL Database using Python 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 th 3 min read How to write Pandas DataFrame to PostgreSQL table? In this article, we will be looking at some methods to write Pandas dataframes to PostgreSQL tables in the Python. Method 1: Using to_sql() function to_sql function is used to write the given dataframe to a SQL database. Syntax df.to_sql('data', con=conn, if_exists='replace', index=False) Parameter 3 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 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 Retrieve Image and File stored as a BLOB from MySQL Table using Python Prerequisites: MySQL server should be installed In this post, we will be talking about how we can store files like images, text files, and other file formats into a MySQL table from a python script. Sometimes, just like other information, we need to store images and files into our database and provi 3 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 Python PostgreSQL - Select Data In this article, we are going to see how to use select data using Python in PostgreSQL and psycopg2. Installation Open the command prompt and write the command given below. pip install psycopg2 SELECT statement is used to retrieve the required details of an existing table in PostgreSQL. The data tha 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 How to read image from SQL using Python? In this article, we are going to discuss how to read an image or file from SQL using python. For doing the practical implementation, We will use MySQL database.  First, We need to connect our Python Program with MySQL database. For doing this task, we need to follow these below steps: Steps to Conne 3 min read Like