Lots of fixes for postgres