Postgresql – Run lo_export as client through psycopg2

exportpostgresqlpython

Python 2.7
psycopg2

When I run:
cur.execute("SELECT lo_export(123456, '/tmp/test.jpg')")

Produces error – need to be superuser to do this.

This makes sense as it is running a server side lo_export and the permissions are not set to superuser for the user I want to run the request.

I want to run a client side lo_export as if I was running psql.

Can I do this within an open psycopg2 connection?

Best Answer

OK simple solution. Use:

conn = psycopg2.connect("db=dbname etc etc")  
lo = conn.lobject(12345)  
lo.export('/tmp/test.jpg')