osm2pgsql is designed for importing data for rendering, not for routing. It's a "lossy" import that doesn't preserve the topology required for routing. (Strictly speaking you could do this if you've imported in slim mode, but it's not the intention.)
For routing, you need to use an importer/pre-processor that will create a routable graph from the OSM data. Routing engines such as OSRM and Graphhopper will do this as part of their preparation process. If you want to write your own routing engine, but are looking for something that will create a graph for you, https://github.com/Tristramg/osm4routing2 is probably the easiest tool to use.use.
(If you're determined to do it in Postgres, check out https://pgrouting.org/docs/tools/osm2pgrouting.html .)