3

I want to tell my ogr2ogr command which inserts data from a ESRI Shapefile into a PostgreSQL/PostGIS database to create the id field as a PostgreSQL IDENTITY column (not as a SERIAL).

For the moment, my command:

$ ogr2ogr -f "PostgreSQL" PG:"postgresql://postgres:password@localhost:5432/mydatabase" \
 myshapefile.shp \
 -lco GEOMETRY_NAME=geom \
 -lco FID=id \
 -lco SPATIAL_INDEX=GIST \
 -nlt PROMOTE_TO_MULTI \
 -nln foo \
 -overwrite

creates the table as follow, creating the id as a SERIAL:

-- Table: public.foo
-- DROP TABLE IF EXISTS public.foo;
CREATE TABLE IF NOT EXISTS public.foo
(
 id integer NOT NULL DEFAULT nextval('foo_id_seq'::regclass),
 fid_part numeric(12,0),
 orient numeric(4,0),
 geom geometry(MultiLineStringZ),
 CONSTRAINT foo_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.foo
 OWNER to postgres;
-- Index: foo_geom_geom_idx
-- DROP INDEX IF EXISTS public.foo_geom_geom_idx;
CREATE INDEX IF NOT EXISTS foo_geom_geom_idx
 ON public.foo USING gist
 (geom)
 TABLESPACE pg_default;

But if I create the same table using pg:

CREATE TABLE IF NOT EXISTS public.foo
(
 id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
 fid_part numeric(12,0),
 orient numeric(4,0),
 geom geometry(MultiLineStringZ)
)

it creates the table as:

-- Table: public.foo
-- DROP TABLE IF EXISTS public.foo;
CREATE TABLE IF NOT EXISTS public.foo
(
 id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
 fid_part numeric(12,0),
 orient numeric(4,0),
 geom geometry(MultiLineStringZ),
 CONSTRAINT foo_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.foo
 OWNER to postgres;

How could I achieve the same result with ogr2ogr?

asked Dec 9, 2022 at 10:57

1 Answer 1

4

You can't create FID as identity with ogr2ogr. In the source code https://github.com/OSGeo/gdal/blob/master/ogr/ogrsf_frmts/pg/ogrpgdatasource.cpp the alternatives for the datatype of FID are either SERIAL (default) or BIGSERIAL (if layer creation option FID64 is TRUE).

/* -------------------------------------------------------------------- */
/* Create a basic table with the FID. Also include the */
/* geometry if this is not a PostGIS enabled table. */
/* -------------------------------------------------------------------- */
 const bool bFID64 = CPLFetchBool(papszOptions, "FID64", false);
 const char* pszSerialType = bFID64 ? "BIGSERIAL": "SERIAL";

Of course if you only want to create the table with custom parameters it is possible to do with ogrinfo that can execute any SQL.

ogrinfo PG:"host=localhost port=5432 dbname=my_db user=user password=pw" \
 -sql "CREATE TABLE IF NOT EXISTS public.foo (
 id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
 fid_part numeric(12,0),
 orient numeric(4,0),
 geom geometry(MultiLineStringZ),
 CONSTRAINT foo_pkey PRIMARY KEY (id)
 )
 TABLESPACE pg_default;"

Once the table is created it is possible to insert data into now existing table with ogr2ogr by using option -append. I made a simple test and it seemed to be successful. New rows were inserted with automatically generated id's.

swiss_knight
11.3k9 gold badges57 silver badges140 bronze badges
answered Dec 9, 2022 at 11:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.