Database: clean up lockTables() and add postgres support