Loading a Shapefile into PostGISΒΆ

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. Once postgis is running, enter the following command and press enter to creating a new database named ‘shape’:

    (Linux)
    
    createdb -U geosolutions -T postgis20 shape
    
    or
    
    (Windows)
    
    setenv.bat
    createdb -U geosolutions -T postgis20 shape
    
  3. Then enter the following command and press enter to load the ShapeFile into ‘shape’ database:

    (Linux)
    
    shp2pgsql -I ${TRAINING_ROOT}/data/user_data/Mainrd.shp public.main_roads | psql -d shape
    
    or
    
    (Windows)
    
    shp2pgsql -I "%TRAINING_ROOT%\data\user_data\Mainrd.shp" public.main_roads | psql -U postgres -d shape
    

    The ShapeFile will be loaded within the ‘main_roads’ 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_postgis1.png

    A PostGIS table by ShapeFile

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