Possibilistic knowledge bases gather propositional formulas associated with degrees belonging to a linearly ordered scale. These degrees reflect certainty or priority, depending if the formulas encode pieces of beliefs or goals to be pursued. Possibilistic logic provides a simple format that turns to be useful for handling qualitative uncertainty, exceptions or preferences. The main result of the paper provides a way for compiling a possibilistic knowledge base in order to be able to process inference from it in polynomial time. The procedure is based on a symbolic treatment of the degrees under the form of sorted literals and on the idea of forgetting variables. The number of sorted literals that are added corresponds exactly to the number of priority levels existing in the base, and the number of binary clauses added in the compilation is also equal to this number of levels. The resulting extra compilation cost is very low.