Retrieving 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:
- MP4
- DOCS
- Image
- Video
- gif
- HTML
- MP3
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:
How to Retrieve Blob Datatype from Postgres with Python
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.