Postgresql – query two point values from a table then insert a new record after creating a line from the two points

postgispostgresql

I am running postgres: 9.5.19

I have already point values as separate records in a table. I want to create a new record which represents a line geom derived from the two points above. I have lots of point records but I want to be able to select by id each point to link together.

I am trying this syntax:

insert into locations (geom,type,label,user) 
select st_makeline(a.geom,b.geom) as line from (select * from locations where id = 144) as a, (select * from locations where id = 145) as b, 
'Link','test','ADMIN';

and get this error:

ERROR: syntax error at or near "'Link'"
LINE 1: …a, (select * from locations where id = 145) as b, 'Link','te…

Can someone point me to a working syntax?

Best Answer

You have to put the literal strings in the right spot. You have them as part of the FROM clause, which doesn't make sense. Put them in the select-list.

insert into locations (geom,type,label,user) select 
    st_makeline(a.geom,b.geom) as line, 'Link','test','ADMIN'
from (select * from locations where id = 144) as a, 
     (select * from locations where id = 145) as b;