How to save POINTS (LANG, LAT) in a geometry type column in PostGIS?

I created a table itapp_citiesin PostGIS that stores data about cities. I added a column locationwith the data type geometryfor storage longitudeand latitudecity. When I run the following query INSERT, I get the error shown below.

INSERT request:

INSERT INTO itapp_cities(city_id, city_name, city_code, state_id, location) 
  VALUES (DEFAULT,'Ada', 'ada-ok',37,POINT(34.774531000000003, -96.678344899999999)); 

Table definition:

CREATE TABLE itapp_cities
(
  city_id bigserial NOT NULL,
  city_name character varying(100) NOT NULL,
  city_code character varying(5) NOT NULL DEFAULT ''::character varying,
  state_id bigint NOT NULL,
  location geometry,
  CONSTRAINT itapp_cities_pkey PRIMARY KEY (city_id),
  CONSTRAINT fk_states FOREIGN KEY (city_id)
      REFERENCES itapp_states (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE
)

Mistake:

ERROR:  column "location" is of type geometry but expression is of type point
LINE 2:   VALUES (DEFAULT,'Ada', 'ada-ok',37,POINT(34.77453100000000...
                                             ^
HINT:  You will need to rewrite or cast the expression.
********** Error **********

ERROR: column "location" is of type geometry but expression is of type point
SQL state: 42804

How to save point values ​​inside this column? I'm new to PostGIS, so forgive me for this stupid question.

+4
source share
2 answers

ST_MakePoint() SRID ( ) ST_SetSRID():

SELECT ST_SetSRID(ST_MakePoint(longitude, latitude),4326)

, , ST_GeomFromText():

SELECT ST_GeomFromText('SRID=4326;POINT(34.774531 -96.6783449)')

dba.SE :

+2

SQL sql

    INSERT INTO itapp_cities(city_id, city_name, slug, state_id, location) 
  VALUES (DEFAULT,'Ada', 'ada-ok',37,st_GeomFromText('POINT(34.774531000000003 -96.678344899999999)', 312));

+2

Source: https://habr.com/ru/post/1612189/


All Articles