How can I repair this leaning deck railing?
If you were to replace all the lag bolts, you could play around with plumb by shimming the top or bottom of the post where it sits on the rim joist. Be sure to use galvanized bolts (and healthy sized washers on both ends) that go all the way through the post and the rim joist.

A very big caveat: this might have met code where you are, but a 2x4 post -- especially an old one -- isn't the sturdiest. The IRC requires that a railing can withstand a 200 pound load in any direction. I would think seriously about replacing the posts with 4x4s securely bolted to the rim.