Consider an electrostatic field ‘E’ due to a charge Q placed at the origin. Now, imagine that we bring a test charge q from a point R to a point P against the repulsive force on it due to charge ‘Q’, without any acceleration. (i.e. we apply an external force. Fext just enough to counter the electrostatic repulsive force F).
Thus, work done by external force in moving a charge q from R to P.

This work done is against electrostatic repulsive force F and is path independent. At every point, test charge q possesses certain potential energy, this work done increases its potential energy.
Thus, potential energy difference –
ΔU = Up - UR = wRP

Therefore we can define electric potential energy difference between two points as the work required to be done by an external force in moving (without acceleration) charge q from one point to another for electric field of any arbitary charge configuration.
A convenient choice is to have electrostatic potential energy zero at infinity with his choice if we take point ‘R’ at infinity, we can write.

Thus, potential energy of a charge q at a point (in the presence of field due to any charge configuration) is the work done by the external force (equal and opposite to electrostatic force) in bringing the charge q from infinity to that point.