A gelling formulation containing ketamine or a pharmaceutically acceptable salt thereof in water, a poloxamer and one or more pharmaceutically acceptable mucoadhesive agents is provided. Also provided is a method of using the formulation for pain treatment.