Copy geometry data as HEXEWKB from csv to table in PostGIS

When you see an error like:

st_setsrid(unknown, integer)

it doesn't mean the ST_SetSRID does not exist, it means that there is a problem with the argument type unknown.

So, you can fix this by casting the HexEWKB directly to a geometry, using the cast operator, ::, and then call ST_SetSRID on that:

INSERT INTO  tout.battest(geom) VALUES 
(ST_SetSRID('010600000001000000010300000001000000050000007041F528CB332C413B509BE9710A594134371E05CC332C4111F40B87720A594147E56566CD332C4198DF5D7F720A594185EF3C8ACC332C41C03BEDE1710A59417041F528CB332C413B509BE9710A5941'
::geometry, 2154));

This works because the HexWBK is already a geometry representation, but in a string format, hence the need for a cast (see this Stack Overflow answer explaining the :: operator as a shortcut for cast). This also means that you do not need any of the ST_From...WKB type operators.

If you want to see the geometry directly, just use ST_AsText, again demonstrating that it is already a direct representation of a geometry.

SELECT ST_AsText('010600000001000000010300000001000000050000007041F528CB332C413B509BE9710A594134371E05CC332C4111F40B87720A594147E56566CD332C4198DF5D7F720A594185EF3C8ACC332C41C03BEDE1710A59417041F528CB332C413B509BE9710A5941');

which returns:

MULTIPOLYGON(((924133.579996152 6564295.65010458,924134.009996152 6564298.11010458,924134.699996152 6564297.99010458,924134.269996152 6564295.53010458,924133.579996152 6564295.65010458)))

EDIT: It is worth mentioning that as you mention CSV in the title, if you have lots of rows to insert, it might be quicker to run the COPY command on the CSV file to load the data into a geometry (mulipolygon) column with no SRID set and then use:

ALTER TABLE 'battest' ALTER COLUMN geom TYPE geometry(MULTIPOLYGON, 2154) USING ST_SetSRID(geom, 2154)

where you need the using clause to prevent unknown SRID errors in the alter table part.


Check the docs. Here's the relevant page: http://www.postgis.net/docs/ST_GeomFromWKB.html

You want something like:

insert into tout.battest(geom) values ST_GeomFromWKB('010600000001000000010300000001000000050000007041F528CB332C413B509BE9710A594134371E05CC332C4111F40B87720A594147E56566CD332C4198DF5D7F720A594185EF3C8ACC332C41C03BEDE1710A59417041F528CB332C413B509BE9710A5941', 2154);

You need to tell PostGIS what you're inserting, as something with '' quotes just looks like a string. Since you've exported Well Known Binary, you'd better insert it as WKB! So, we use the function ST_GeomFromWKB(bytea geom, integer srid) to do it.

EDIT: this answer is incorrect. Since this is a HexWKB, you can cast it to a geometry type with:

ST_SetSRID('010600000001000000010300000001000000050000007041F528CB332C413B509BE9710A594134371E05CC332C4111F40B87720A594147E56566CD332C4198DF5D7F720A594185EF3C8ACC332C41C03BEDE1710A59417041F528CB332C413B509BE9710A5941'::geometry, 2154)