I have got two postdoc offers, one with the official offer letter in hand and the other still pending for the letter. I am considering to turn down the first one. Since during previous emails with the PI who sent the offer letter to me, I have indicated to accept the offer, although I haven't put my name on the letter yet. At this point, is it appropriate for me turn down that offer?
The appropriate time is after you get the other (better) offer in writing. You don't want to turn something known down for something else that hasn't come yet, formally.
Yes, it is unfortunate to have said you will take the position and turn it down, but these things happen. Certainly in business, there are lots of times when agreed to deals still fail when the time comes to put pen to paper. It is not the end of the world. Just be matter of fact (not penitent, not rebellious) and move forward with whatever is the best fit for you.
P.s. This is why I emphasize to interview widely and job search arduously, all the way up to having gotten a written offer and accepted it. You're not off the market until then. The same thing applies in reverse as verbal offers may not always materialize (check out lots of questions/complaints about this on this site). I'm not advocating being deceptive or fickle. But you certainly want to look for your best situation and control the options as much and as long as you can. Employers do the same in reverse. It is a bit of a mating dance.