0

I'm composing a liquibase script to create a table with Geometry column (from PostGis extenstion of PostGreSQL DB)

However, I couldn't manage to get it working. My script:

  - changeSet:
      id: 5-change-set-places
      comment: Create places table
      author: LongLe
      validCheckSum: 1:any
      changes:
        - sql:
            dbms: postgresql
            endDelimiter: ;
            splitStatements: true
            sql: |
              CREATE TABLE IF NOT EXISTS "${schema}"."places" (
                "id" bigint NOT NULL,
                "geom" GEOMETRY(Geometry, 4326) NOT NULL
              );
              GRANT SELECT, INSERT, UPDATE, DELETE ON "${schema}"."places" TO "${rolename}";
              ALTER TABLE "${schema}"."places"
                DROP CONSTRAINT IF EXISTS "places_pkey",
                ADD CONSTRAINT "places_pkey" PRIMARY KEY ("id");

However, it failed with below error:

ERROR: type "geometry" does not exist

I also referred to liquibase-spatial extension https://github.com/lonnyj/liquibase-spatial and compose the equivalent config:

  - changeSet:
      id: 5-change-set-places
      comment: Create places table
      author: LongLe
      validCheckSum: 1:any
      changes:
        - createTable:
            columns:
              - column:
                  constraints:
                    nullable: false
                    primaryKey: true
                    primaryKeyName: places_pkey
                  name: id
                  type: bigint
              - column:
                  constraints:
                    nullable: false
                  name: geom
                  type: GEOMETRY(Geometry,4326)
            tableName: test

But it still failed with the error:

31-03-2021 21:07:31.587 [main] WARN c.m.ms.block...applyLiquibase - Error applying liquibase attempt 5 : Migration failed for change set classpath:db/changelog/db.changelog-master.yaml::5-change-set-places::LongLe: Reason: liquibase.exception.DatabaseException: ERROR: type "geometry" does not exist Position: 67 [Failed SQL: CREATE TABLE testschema.places (id BIGINT NOT NULL, geom geometry(Geometry, 4326) NOT NULL, CONSTRAINT places_pkey PRIMARY KEY (id))]

But when I took out the SQL from the log to run:

CREATE TABLE testschema.places (id BIGINT NOT NULL, geom geometry(Geometry, 4326) NOT NULL, CONSTRAINT places_pkey PRIMARY KEY (id))

Then it works just fine.

I must have missed some very simple thing, but I checked up & down without any clue. Anyone has a better idea?

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124
  • 1
    Not sure but just a thought. Have you tried "CREATE EXTENSION postgis;" before using "geometry" from the extension? – Rakhi Agrawal Apr 01 '21 at 05:39
  • Hi @RakhiAgrawal, yes, I do install PostGis before. Having added that in the previous script still produces the same issue – Hoàng Long Apr 01 '21 at 06:29
  • Hi @LaurenzAlbe Thanks! This turn out to be the issue. I added one SQL command to include "public" into the search path and it works – Hoàng Long Apr 01 '21 at 06:30

1 Answers1

1

Make sure you have run

CREATE EXTENSION postgis;

Then schema qualify the data type. If the extension was created in public, use

CREATE TABLE places (geom public.geometry, ...);
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263