ST_Azimuth to get the angle from the origin point to the point of interest. I've used simple
geometry points here since you didn't have any sample data, but you're probably working with
geography. The principle is the same:
WITH points(star) AS (VALUES
WHERE degrees(ST_Azimuth( point(0,0)::geometry, star::geometry ))
BETWEEN 0 AND 40;
You can also limit distance. Here's how to get distance and azimuth from table
"points" containing a
degrees(ST_Azimuth( point(0,0)::geometry, star::geometry )) AS azimuth_deg,
ST_Distance( point(0,0)::geometry, star::geometry ) AS distance
You can move those into the
WHERE clause and filter on them however you like.
If possible work directly in radians, rather than converting to degrees.
If you're working with points on the earth, consider using the
geography type instead of
geometry. Both functions mentioned here support