Creating polygons from line segments using PostgreSQL and PostGIS

ST_Polygonize will do the job:

CREATE VIEW boundarypolygons AS
SELECT 
   g.path[1] as gid, 
   g.geom::geometry(polygon, 31492) as geom 
FROM
   (SELECT 
     (ST_Dump(ST_Polygonize(geom))).* 
   FROM boundary
) as g;

enter image description here


Since you are using PostGIS and QGIS, you can try to convert them using both, to decide which one is best suited for your problem. To use PostGIS, the LINESTRING must be closed. You can check if they are closed with the query:

select gid, st_isclosed(geom) from boundary;

If the lines are closed, you can create another table to check the results, with:

create table newboundary as
select gid, ST_MakePolygon(geom) 
from boundary
where st_isclosed(geom);

Add the newboundary to QGIS afterwards.

To use QGIS, check Create polygon layer from polyline layer.