Data type to store Postgresql coordinates

1

Good evening I have a database in Postgresql and I want to add two fields to store latitude and longitude, I read in several blogs that the field should be floating (float), and my question in particular is whether this statement is correct and if it is that dimfnsion should have ??

    
asked by ALVARO ROBERTO BACARREZA ARZAB 06.08.2018 в 00:51
source

1 answer

1

You should use DECIMAL or in the case of PostgreSQL its equivalent is NUMERIC since it will allow you to do mathematical operations on the data you store

An example of the declaration should be like this:

CREATE TABLE ubicaciones(
   ubicacion VARCHAR(10),
   latitud NUMERIC(10,8),
   longitud NUMERIC(10,8)
 );

Since being coordinates you will need to calculate: distance, averages, etc.

As you yourself can check in the documentation for the type area says

  • NUMERIC = > It is an exact data of a selectable precision
  • DOUBLE = > Double precision floating point number
  • So I see that double can become ambiguous and risky to use it

    Even another option is that through PostgreSQL you use JSON data to save both records in the same column, I'll give you an example

    CREATE TABLE paises(
       ubicacion VARCHAR(10),
       precisiones JSON
     );
    

    Later you would insert the data in this way

    INSERT INTO paises(ubicacion, donde) 
    VALUES('Mexico', '{"latitud": "19.1571124", "longitud": "19.4875414"}');
    

    And finally reading them would look like this:

    SELECT * FROM datos;
    
    ubicacion   donde
    Mexico      {"latitud": "19.1571124", "longitud": "19.4875414"}
    
        
    answered by 06.08.2018 / 01:09
    source