Thank you very much for your answer!
I post the full solution to anyone else maybe need it:
try:
        conn = psycopg2.connect("dbname='...' user='...' host='127.0.0.1' password='....'");
except:
        print "I am unable to connect to the database"
curs = conn.cursor()
........ code to create image/graph....... #canvas contains graph
import StringIO
data=StringIO.StringIO()
canvas.print_figure(data, dpi=150, format="png")
sql = "update table set image=%s;"
curs.execute(sql,(encoded_data,))
conn.commit()



2009/12/19 Jae-Joon Lee <lee.j.joon@gmail.com>
On Fri, Dec 18, 2009 at 3:06 AM, Panagiotis Kontaxis <pkontaxi@gmail.com> wrote:
> I found only the figure.savefig() function that saves figure's image only as
> a filename in the filesystem and not in a local variable.


http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig

savefig can take any file-like object, not just a file name.

Check your matplotlib version, and upgrade if you can.

Regards,

-JJ