Topological insulators (TIs) have attracted much attention due to their spin-polarized surface and edge states, whose origin in symmetry gives them intriguing quantum-mechanical properties. Robust control over the chemical potential of TI materials is important if these states are to become useful in new technologies, or as a venue for exotic physics. Unfortunately, chemical potential tuning is challenging in TIs in part because the fabrication of electrostatic top-gates tends to degrade material properties and the addition of chemical dopants or adsorbates can cause unwanted disorder. Here, we present an all-optical technique which allows persistent, bidirectional gating of a (Bi,Sb)2Te3 channel by optically manipulating the distribution of electric charge below its interface with an insulating SrTiO3 substrate. In this fashion we optically pattern p-n junctions in a TI material, which we subsequently image using scanning photocurrent microscopy. The ability to dynamically write and re-write mesoscopic electronic structures in a TI may aid in the investigation of the unique properties of the topological insulating phase. The optical gating effect may be adaptable to other material systems, providing a more general mechanism for reconfigurable electronics.