I am trying to import GeoJSON file with Python to my PostgreSQL db.

Here is full code:

with open('C:/xampp/htdocs/app/data/'+a) as file:
for feature in gj['features']:
    cur.execute('INSERT INTO dkp (id,geom,ko_broj,kc_broj,ko_naziv,jls) VALUES (%s, ST_GeomFromText(ST_AsText(ST_GeomFromGeoJSON(%s))), %s, %s, %s, %s)', (id, geom ,ko_broj, kc_broj, ko_naziv, jls,))

And I get error :

psycopg2.ProgrammingError: can't adapt type 'Polygon'

Does anyone know how to fix this?

  • In the insert command some apostrophes missing. Print your Insert command from Python and try to execute in psql or pgadmin. – Zoltan Feb 3 at 13:38
  • @Zoltan can you underline where apostrophes are missing because when I copy this command into sql command window it works fine – Goran Grle Feb 3 at 14:04
  • If it works from the SQL prompt then my assumption was false. – Zoltan Feb 3 at 14:26

Insert statement seems OK. But you could try to replace this line:



geom = (json.dumps(feature['geometry']))))

Because feature['geometry'] is an object and you should convert it to json.

And in insert statement you should set SRID for geometry if you have constraint in PostGIS. Like this:

ST_SetSRID(ST_GeomFromGeoJSON(%s), 4326)
  • It works, thank you ! – Goran Grle Feb 3 at 14:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.