The 'role' that your users assume in Postgres are local to the database. Your role in the database enforces database permissions. So, you can say that foobar can run this stored proc, or select this table, and Postgres will enforce that. If you create your procedure using SECURITY INVOKER that means that the procedure being run will be run using the current logged in Postgres user. SECURITY DEFINER means that the procedure will be run using the role that created the procedure. Either way, if your procedure does anything outside Postgres (like create a file) it will be done as the identity that started Postgres (as you have found out). In my case, I have a Unix user named 'postgres' and when I start up Postgres I do it as that user. So, any file that is created will be owned by the 'postgres' user.
You didn't tell us your operating system. Windows will be different than *nix. This answer is for *nix.
You did mention that your stored proc language was plpython. So, you could create the file, then change it's ownership after you create it in your procedure. Like:
import os
import pwd
f = open('/tmp/myfile','w')
f.write('hello')
f.close()
user_id = pwd.getpwnam("foobar").pw_uid
os.chown('/tmp/myfile', user_id, -1)
Assuming that there is a foobar user on the host that runs Postgres this should work.
-g