Simply load data into PostGIS database with osm2pgsql functions and use PostGIS functions (for which there are many examples of your use cases - just search with Google.)

Just keep an eye for the issues with distances in different geographical projections. PostGIS has geography type, which can help.