port back to postgres.py

Let's get a PR going on this one. This addresses #1146.