How should I connect 2x4 (or wood horizontal railing pieces) to exterior painted wood?
You call it a patio, so I assume it is not a wood deck (which would be easier to add a railing). I'd avoid attaching it to the house though it seems that the other end of the patio roof may be tied to the house already.
Consider driving a super spike into the ground at each end to hold a post that will be the ends of the railing. At a 10 foot span, you'll want one in the center of those two as well. If driving the spike just off the patio won't work, then remove a patio stone and drive the spike where you need it. You can break/ cut the patio stone to fit around the post once its in place.