refactored database code to use the postgres.py wrapper

also typo fix