Fix postgres queries