.. _geoserver.postgis.postgis_load:

Loading Spatial Data
--------------------

There are a variety of ways to load a PostGIS database with spatial data. This section covers the task of loading a database with spatial data via SQL scripts loaded with pgAdmin.

#. In the :guilabel:`Object Browser` navigate to :menuselection:`Databases --> bayarea`, and click the :guilabel:`SQL Query button` in the toolbar.

   .. figure:: postgis_load4.jpg
      :width: 650
      
      Selecting a database and executing an SQL query

#. In the :guilabel:`Query` window, open the :guilabel:`File` menu and select :guilabel:`Open`.

   .. figure:: postgis_load5.jpg
      :width: 650

      Opening an SQL script

#. In the :guilabel:`File Explorer` navigate to the workshop :file:`data/bayarea` directory and open the :file:`bay_area_roads.sql` file.

   .. figure:: postgis_load6.jpg

      Selecting the bay_area_roads SQL script

   .. figure:: postgis_load7.jpg

      The bay_area_roads SQL script

   .. note::  The :file:`bay_area_roads.sql` file contains the SQL statements to create a table named bay_area_roads and populate it with data.

   .. warning:: It may take a few moments for pgAdmin to load the :file:`bay_area_roads` script. Be patient.

#. Execute the SQL by clicking the :guilabel:`Execute Query` button in the toolbar.

   .. figure:: postgis_load8.jpg

      Executing an SQL script

   Upon successful query execution the following status message should appear in the **Messages** panel:

   .. figure:: postgis_load9.jpg

      Successful SQL completion

#. Repeat steps 5 through 7 with the :file:`bay_area_transit.sql` script

   .. note:: The :file:`bay_area_transit.sql` creates the tables:

        * ``bay_area_bus_stops``
        * ``bay_area_transit_stations``
        * ``bay_area_transit_routes``

#. Verify that all the tables are loaded via the pgAdmin :guilabel:`Object Browser`:

   .. figure:: postgis_load10.jpg

      Browsing tables in the bayarea database

   .. note:: It may be necessary to **refresh** the bayarea database in the object browser before the new tables are visible.

        .. figure:: postgis_load11.jpg

           Refreshing the database

At this point the PostGIS database has been created and is loaded with spatial data. The next few sections will focus on connecting to PostGIS with GeoServer and serving the data sets created in this section.