Search code examples
python-3.xpostgispsycopg2postgresql-9.3geopy

Geometry value into PostgreSQL+PostGIS


I have a problem to insert a geometry value into a PostgreSQL/PostGIS database. I'm using psycopg2 package. I want to store latitude and longitude, that I get using geopy library, with the next code:

geolocator = Nominatim()
location = geolocator.geocode(name)

I'm very confused about what SQL sentence I have to write. I'm trying

sql = """INSERT INTO Sensor(name, value, location, unit, frecuencyUpdate)
                 VALUES(%s, %s, ST_MakePoint(%s, %s),%s, %s)
                 RETURNING id;"""

 cur = conn.cursor()
 cur.execute(sql, (sensor.estacion, sensor.waterState, sensor.getLongitude(), sensor.getLatitude(), sensor.unit, sensor.frecuencyUpdate))

And I get the error:

"doesn't exits relation «sensor»"

Can anybody help me?


Solution

  • Your SQL has a capital S for the table name but it is not quoted, so Postgres is looking for a table with lower case letters. Should you have created the table using the capital letter, you must quote it:

    sql = """INSERT INTO \"Sensor\"(name, value, location, unit, frecuencyUpdate)
                     VALUES(%s, %s, ST_MakePoint(%s, %s),%s, %s)
                     RETURNING id;"""