Postdocs are just like a job, so they can be advertised at any time, there is no consensus that I am aware of.
People say you should start applying 6 months before the end of your PhD. I think that seems a lot of time will be spent job searching when one should be thesis writing, so I am going to wait until I am close to submitting before I start looking seriously for anything. Personally I want a gap between the end of one lot of research and the start of another because that makes more sense to me (I'm a 'completer-finisher' type of person).
It's often much more about 'who you know' or 'who your supervisor knows' that 'what you know'. You can apply randomly, and sometimes will get the position though obviously.
In some countries, mainly less developed ones, a PhD is all you need to secure a lectureship. In the US and UK, you need teaching experience for a lectureship with no research, and for a research position (where you probably need to lecture too) you need many years of a postdoc and a great publication record to even be in with a chance.
Neither are easy to get in to, due to the high number of candidates with PhDs and decreasing funding.