What if I need a pain pill in the middle of the night?

Depends on the drug. Many pain medicines only last 4 hours and therefore repeat dosing at night is acceptable.
Bedside. Keep it by your bedside. If you are trying to take the medication on a schedule, you can set an alarm to wake you up so you can take it. By not missing a dose, you will likely be able to control your pain better.