Adjust SQL in messages_controller to work with Postgres