So if i want to pull out lat/lngs after user clicks on a map canvas (lat/lngs are pulled using any javascript API (leaflet, wax etc.) and add this to a postgresql database using a query, how do i do this?

I came across:


But how do you generally add values to a db on the fly?

3 Answers 3


I might be getting this wrong, but just to check, you aren't planning on using https://github.com/commandprompt/postgres-js on a client/user-facing website are you? It looks like it connects with a password provided, which means you'd be supplying the user with your db password!

All you need to store points that the user clicks is a backend that can insert data as Aragon said, and a simple PUT to e.g. a flask app -

Front end:

map = L.Map(...);
map.on('click', function(e) {
    $.ajax('someurl/putpoint/', {
      type: 'PUT',
      data: {
        lat: e.latlng.lat,
        lng: e.latlng.lng

Back end with e.g. http://flask.pocoo.org/ :

from flask import Flask
app = Flask(__name__)

def putpoint():
    cursor = get_your_db_cursor()
    # NB: massively insecure accepting any random input from PUT!
    # Should use the params arg in execute() rather than format str
        INSERT INTO test(p_id, the_geom)
        VALUES(2, ST_GeomFromText('POINT(%s %s)', 4326));""" % (
        request.form['lng'], request.form['lat']))  

if __name__ == '__main__':

did you implement this insert function to js library you give?

PostGIS adding geomerty function:

 INSERT INTO test(p_id, the_geom)
 VALUES(2, ST_GeomFromText('POINT(10 20)', 4326));

Your postgres backend protocol insert function:

 db.prepare("INSERT INTO returning_test (val) VALUES (?) RETURNING id, val", function (sth) {
 sth.execute("text value", function(e, rs) {
 if (rs === undefined) {
 console.log("No data.");
 else {

i hope it helps you...


To add values to a datebase on the fly, break it up in three steps:

1) Catch the click and position in Javascript:


2) Send the position to a server using Ajax:


3) Save the values in the database using PHP:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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