We carry out a systematic study of the static and dynamic polarizabilities of the potassium atom using a first-principles high-precision relativistic all-order method in which all single, double, and partial triple excitations of the Dirac-Fock wave functions are included to all orders of perturbation theory. Recommended values and uncertainties are provided for the relevant electric-dipole matrix elements. Polarizabilities of the 4s, 4p(j), 5s, 5p(j), and 3d(j) states are compared with other theoretical and experimental values when possible. We identify magic wavelengths for the 4s-np transitions for n = 4,5, i.e., those wavelengths for which the two levels have the same ac Stark shifts. The magic wavelengths for the 4s-5p transitions are of particular interest for attaining a quantum gas of potassium at high phase-space density. We find 20 such wavelengths in the technically interesting region of 1050-1130 nm.