Loading a Shapefile into PostGIS - ogr2ogrΒΆ

This task shows how to load a ShapeFile into PostGIS database:

  1. Ensure that postgis server is running. If not, open the terminal window and start it:

    (Windows)
    
          postgis_start.bat
    
  2. Then enter the following command and press enter to load the ShapeFile into ‘shape’ database:

    (Linux)
    ogr2ogr -f PostgreSQL PG:"dbname='shape' host='127.0.0.1' port='5434' user='geosolutions' password='Geos'" ../data/user_data/Mainrd.shp -lco GEOMETRY_NAME=geom -lco FID=gid -lco SPATIAL_INDEX=GIST -nlt PROMOTE_TO_MULTI -nln main_roads_2 -overwrite
    
    (Windows)
    ogr2ogr -f PostgreSQL PG:"dbname='shape' host='127.0.0.1' port='5434' user='geosolutions' password='Geos'" ..\data\user_data\Mainrd.shp -lco GEOMETRY_NAME=geom -lco FID=gid -lco SPATIAL_INDEX=GIST -nlt PROMOTE_TO_MULTI -nln main_roads_2 -overwrite
    

Where:

  • -f PostgreSQL: specifies the output format as PostgreSql table
  • -lco GEOMETRY_NAME: (layer creation option) specifies the name of the geometry field
  • -lco FID: (layer creation option) specifies the name of the feature identifier column to create
  • -lco SPATIAL_INDEX: (layer creation option) specifies the type of spatial index (available values are: NONE/GIST/SPGIST/BRIN)

The ShapeFile will be loaded within the ‘main_roads_2’ table of the ‘shape’ database.

The following screenshot shows some of the table contents in pgAdmin.

Note

In Windows, you can run pgAdmin, executing pgAdmin.bat batch file at terminal window.

../_images/shp_postgis2.png

A PostGIS table by ShapeFile

In the next section we will see how to add a PostGIS layer into GeoServer.