try this:
select substring('https://dev.foo.com/bar/action/?param=val' from '(https?://([^/]*/){1,2})');
template1=# select substring('https://dev.foo.com/bar/action/?param=val' from '(https?://([^/]*/){1,2})');
substring
-------------------------
https://dev.foo.com/bar/
(1 row)
template1=# select substring('http://dev.foo.com/bar/action/?param=val' from '(https?://([^/]*/){1,2})');
substring
------------------------
http://dev.foo.com/bar/