Riding your bike should not be causing you pain. At least, not long-term pain anyway.
If you get off your bike after a ride with sore knees, or even a sore lower back, chances are your saddle is at the wrong height. Have your saddle too high and your pelvis will be rocking from side to side as you pedal, causing discomfort in the lower back and pelvis. Have it too low and you’ll likely end up with sore knees.
If you have competitive aspirations, it may be worth spending the money on a proper bike fit. In Cardiff, the Spire (it used to be Bupa for those of us advancing in our years…) does an advanced bike fit for £175, but we’ve not tried them nor are we endorsing them. Your local bike shop should also be able to help you out if you buy a bike from them.
Whilst you can spend quite a bit of on a bike fit to get your saddle height set professionally, this is perhaps a waste of money for the vast majority of us. There is however a fairly easy method to at least get you close to the right height.
The heel-to-pedal method is one that has worked well for me. Place your bike up against the wall, or on a turbo trainer if you have one. With your cycling shoes on, sit on the bike and get yourself comfortable.
Now, place your heels on the pedals and pedal backwards (or forwards if you’re on a turbo) until the right crank is pointing straight down (in the 6:00 position). Your knee should be locked out at this point, so if it isn’t, raise your saddle slightly and repeat the process.
Once you are happy that your knee is locked out, pedal backwards with your heels still on the pedals and make sure your hips are not rocking from side to side. You may need a friend to help you here. If they are, lower your saddle a millimetre at a time until you are happy.
Go for a ride and see how you get on with it.
You may also need to consider the saddle offset, as it can shift forwards and backwards on its rails to increase or reduce the reach to the handlebars, as well as the angles your shins will be at as you pedal.
There’s no hard & fast rule here, but generally speaking, with your pedals at the 3:00/9:00 position, the back of your knee on the leading leg should be roughly over the middle of the crank arm.