I have two tables in the PostGIS database - points and lines. And I want to update points geometry to snap them to line if they lay within a certain distance.
By now I managed to create a new table with snapped points which has only geometries. This works:
CREATE TABLE new_table AS
SELECT ST_ClosestPoint(lines.geom, points.geom) as snapped_point
FROM lines
INNER JOIN points on ST_Dwithin(lines.geom, points.geom, 5)
ORDER BY points.id;
But what I really need is to update the existing table. So I try:
UPDATE points
SET geom =
(SELECT ST_ClosestPoint(lines.geom, points.geom) as snapped_point
FROM lines
INNER JOIN points on ST_Dwithin(lines.geom, points.geom, 5)
ORDER BY points.id);
And it did not work. How do I put these two pieces together?