Creating a Spatial Table in PostGIS
April 5th, 2007
Creating the spatial table from scratch…
[postgres@rupert-linux ~]$ su - postgres [postgres@rupert-linux ~]$ createdb _E UTF-8 template_postgis CREATE DATABASE [postgres@rupert-linux ~]$ createlang plpgsql template_postgis [postgres@rupert-linux ~]$ psql -f /usr/local/pgsql/share/lwpostgis.sql -d template_postgis CREATE FUNCTION CREATE FUNCTION CREATE FUNCTION CREATE FUNCTION CREATE FUNCTION COMMIT postgres@rupert-linux ~]$ psql -f /usr/local/pgsql/share/spatial_ref_sys.sql -d template_postgis [postgres@rupert-linux ~]$ psql
Alternatively you could also create your spatial table from a template…
14 postgres=# CREATE DATABASE gistest TEMPLATE=template_postgis; 15 CREATE DATABASE
I have also added the routing functions using routing.sql and routing_postgis.sql
[postgres@rupert-linux ~]$ psql -f /usr/local/pgsql/share/contrib/routing.sql -d template_postgis [postgres@rupert-linux ~]$ psql -f /usr/local/pgsql/share/contrib/routing_postgis.sql -d template_postgis
FYI…
The spatial_ref_sys table contains the ESPG codes and projections. Currently it has 3162 records.
gistest=# \d spatial_ref_sys; srid | integer | NOT NULL auth_name | character varying(256) | auth_srid | integer | srtext | character varying(2048) | proj4text | character varying(2048) |
The geometry_columns contains the geometry type of the table you just created..
gistest=# \d geometry_columns; f_table_catalog | character varying(256) | NOT NULL f_table_schema | character varying(256) | NOT NULL f_table_name | character varying(256) | NOT NULL f_geometry_column | character varying(256) | NOT NULL coord_dimension | integer | NOT NULL srid | integer | NOT NULL type | character varying(30) | NOT NULL
You need to create spatial indexes on your queries to make it faster. As per postgis docs:
http://pgrouting.postlbs.org/wiki/1.x/InstallationUbuntu710
In Debian/Ubuntu, we need to add routing functions from pgRouting..
# Add pgRouting functions
psql -U postgres -f /usr/share/postlbs/routing_core.sql routing
psql -U postgres -f /usr/share/postlbs/routing_core_wrappers.sql routing