You can try to use gradient descent on the coefficients of $p$. If, say, $p(x) = \sum a_i x^i$, then $$\frac{\partial}{\partial a_k}\int\left(p(x) - f(x)\right)^2\operatorname{dx} = \int 2 (p(x) - f(x))x^k\operatorname{dx}$$
If you can calculate the last integral, you can directly apply gradient descent.