I found this question saving python object in postgres table with pickle in stackoverflow.
I have a python script which creates some objects. I would like to be able to save these objects into my postgres database for use later. Following the comment from @SergioPulgarin I tried the following which worked!
Storing:
- Pickle the object to a binary string pickle_string = pickle.dumps(object)
- Store the pickle string in a bytea (binary) field in postgres. Use simple INSERT query in Psycopg2
Retrieval:
- Select the field in Psycopg2. (simple SELECT query)
- Unpickle the decoded result retrieved_pickle_string = pickle.loads(decoded_result)
I dumped the pickle file. Now I'm trying to load it into the database. My problem is that I cannot find the right syntax to do that...
I've tried it within a python file with psycopg2: I've already dumped the pickle object and connected to the database. I tried to change the datatype to byte and to insert then the pickle into the DB:
pickle_obj = bytes(pickle_file)
query = "INSERT INTO schema.table (col1, ...) VALUES (%%s, ...)" % pickle_obj
cursor.execute(query)
I got this error:
Traceback (most recent call last):
File "/path/to/file.py", line 18, in <module>
cursor.execute(query)
psycopg2.errors.SyntaxError: syntax error at or near "K"
LINE 1: ...e1_h\x15Nh\x16K\x00h\x17Nh\x18Nh\x19\x89h&K\x0bh\'K\x01h(h+h...
^
Process finished with exit code 1
Then I tried it to insert it directly from postgreSQL (But I am not sure about this code either, as it didn't work as well.)
INSERT INTO table SELECT PG_Read_File('/home/...)
I think my syntax isn't 100% correct? So how did you do it? Thanks for your tips!
Try to wrap this object with psycopg2.Binary as stated in the docs. https://www.psycopg.org/docs/usage.html#adapt-binary
The posts seems to be deleted, but as it worked for me I just add it here. Thank you!