nourdine
nourdine

Reputation: 7597

Insert point into a PostGis enabled Postgres db

I have this (non working) query:

"INSERT INTO notes (
         lat, 
         lng, 
         point)
      VALUES (
         :lat,
         :lng,
         ST_GeometryFromText('POINT(:lat :lng)'))"

Which I am running against this table:

CREATE TABLE notes
(
  id serial NOT NULL,
  lat real NOT NULL,
  lng real NOT NULL,
  point point NOT NULL,
  CONSTRAINT notes_pkey PRIMARY KEY (id )
)

The query is prepared with PDO (php) but that is not the point I think. The result I get back is an exception claiming that:

SQLSTATE[42804]: Datatype mismatch: 7 ERROR:  column "point" is of type point but expression is of type geometry
LINE 8:          $8, $9, $10, $11, $12, ST_GeometryFromText('POINT(:...
                                        ^
HINT:  You will need to rewrite or cast the expression.

How can I cast the expression as suggested?

Upvotes: 1

Views: 1706

Answers (2)

Mike T
Mike T

Reputation: 43692

It's not clear if you want to use PostgreSQL's point type, or PostGIS's geometry type. If it is the later, this answer should help out.

Upvotes: 0

Erwin Brandstetter
Erwin Brandstetter

Reputation: 658222

Looks like you are using the standard Postgres type point:

"INSERT INTO notes (lat, lng, point)
 VALUES (:lat, :lng, '(:lat, :lng)'::point)"

You may want to use the PostGis type geometry in your table instead.

Upvotes: 1

Related Questions