Professional Documents
Culture Documents
How It Works
shp2pgsql converts a shapefile into a series of SQL commands that can be loaded into a
databaseit does not perform the actual loading. The output of this command may be
captured in a SQL file, or piped directly to the psql command, which will execute the
commands against a target database.
Preparation
1. Select the shapefile you wish to loadyou will need all the files: .shp , .shx , and .dbf
and so on.
2. Identify the SRID (projection) of your data. If available, this information is easily accessed
via the layer metadata in GeoServer. If the projection is unknown, use a service like
prj2epsg.org to upload and convert the shapefiles .prj file to a SRID code.
3. Either identify the target database where you would like to load the data, or create a new
database.
Loading data
4.1
postgis_version
--------------------------------------2.0 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
Note
These examples use the default of 5432, but substitute your own PostGIS port if
different with the -p option.
3. Run the shp2pgsql command and pipe the output into the psql command to load the
shapefile into the database in one step. The recommended syntax is:
shp2pgsql -I -s <SRID> <PATH/TO/SHAPEFILE> <SCHEMA>.<DBTABLE> | psql -d
<DATABASE>
shapefile)
<DATABASE> Target database where the table will be created
The -I option will create a spatial index after the table is created. This is strongly
recommended for improved performance. For more information about shp2pgsql
command options, please refer to the Using the Loader section of the PostGIS
Documentation.
4. If you want to capture the SQL commands, pipe the output to a file:
shp2pgsql -I -s <SRID> <PATH/TO/SHAPEFILE> <DBTABLE> > SHAPEFILE.sql
The file can be loaded into the database later by executing the following:
psql -d <DATABASE> -f SHAPEFILE.sql
The shapefile has now been imported as a table in your PostGIS database. You can verify this
by either using pgAdmin to view the list of tables, or by executing the following query at the
command line:
psql -U <USERNAME> -d <DATABASE> -c "\d"
Note
The specific command parameters will depend on your local configuration.
Schema |
Name
Type
Owner
--------+----------------------+----------+---------public | bc_2m_border
| table
| postgres
| view
| postgres
public | spatial_ref_sys
| table
| postgres
Batch loading
Although it is feasible to run the shp2pgsql command as many times as required, it may be
more efficient to create a batch file to load a number of shapefiles.
Create a batch file, for example loadfiles.cmd , in the same directory as the shapefiles to be
loaded. Add the following commands and provide the missing parameters:
for %%f in (*.shp) do shp2pgsql -I -s <SRID> %%f %%~nf > %%~nf.sql
for %%f in (*.sql) do psql -d <DATABASE> -f %%f
Run this batch file to load all the selected shapefiles into the database.
Bash
Note
This script also assumes all the files have the same projection.
Create a shell script file, for example loadfiles.sh , in the same directory as the shapefiles to
be loaded. Add the following commands and provide the missing parameters:
#!/bin/bash
for f in *.shp
do
shp2pgsql -I -s <SRID> $f `basename $f .shp` > `basename $f .shp`.sql
done
for f in *.sql
do
psql -d <DATABASE> -f $f
done