shp2pgsql - shapefile to postgis loader
shp2pgsql [options] shapefile [schema.]table
The shp2pgsql data loader converts ESRI Shape files into SQL suitable for insertion into a PostGIS/PostgreSQL database.
Version: 1.1.5 (2006/10/06)
The <shapefile> is the name of the shape file, without any extension information. For example, 'roads' would be the name of the shapefile comprising the 'roads.shp', 'roads.shx', and 'roads.dbf' files.
The <tablename> is the (optionally schema-qualified) name of the database table you want the data stored in in the database. Within that table, the geometry will be placed in the 'geo_value' column by default.
The loader has several operating modes distinguished by command line flags:
(Note that -a, -c, -d and -p are mutually exclusive.)
- Drops the database table before creating a new table with the data in the Shape file.
- Appends data from the Shape file into the database table. Note that to use this option to load multiple files, the files must have the same attributes and same data types.
- Creates a new table and populates it from the Shape file. This is the default mode.
- Only produces the table creation SQL code, without adding any actual data. This can be used if you need to completely separate the table creation and data loading steps.
- Use the PostgreSQL "dump" format for the output data. This can be combined with -a, -c and -d. It is much faster to load than the default "insert" SQL format. Use this for very large data sets.
- Output WKT format, instead of WKB. Note that this can introduce coordinate drifts due to loss of precision.
- Execute each statement on its own, without using a transaction. This allows loading of the majority of good data when there are some bad geometries that generate errors. Note that this cannot be used with the -D flag as the "dump" format always uses a transaction.
- -s [<FROM_SRID>:]<SRID>
- Creates and populates the geometry tables with the specified SRID. If FROM_SRID is given, the geometries will be reprojected. Reprojection cannot be used with -D.
- Use the geography type instead of geometry. Geography is used to store lat/lon data. At the moment the only spatial reference supported is 4326.
- -g <geometry_column>
- Specify the name of the geometry column (mostly useful in append mode).
- Keep identifiers case (column, schema and attributes). Note that attributes in Shapefile are usually all UPPERCASE.
- -m <filename>
- Specify a file containing a set of mappings of (long) column names to 10
character DBF column names. The content of the file is one or more lines
of two names separated by white space and no trailing or leading space:
- Coerce all integers to standard 32-bit integers, do not create 64-bit bigints, even if the DBF header signature appears to warrant it.
- Generate simple Geometries instead of MULTIgeometries. Shape files don't differ between LINESTRINGs and MULTILINESTRINGs, so shp2pgsql generates MULTILINESTRINGs by default. This switch will produce LINESTRINGs instead, but shp2pgsql will fail when it hits a real MULTILINESTRING. The same works for POLYGONs vs. MULTIPOLYGONs.
- -W <encoding>
- Specify the character encoding of Shapefile's attributes. If this option is used the output will be encoded in UTF-8.
- Create a GiST index on the geometry column.
- -N <policy>
- Specify NULL geometries handling policy (insert,skip,abort).
- -T <tablespace>
- Specify the tablespace for the new table. Indexes will still use the default tablespace unless the -X parameter is also used. The PostgreSQL documentation has a good description on when to use custom tablespaces.
- -X <tablespace>
- Specify the tablespace for the new table's indexes. This applies to the primary key index, and the GIST spatial index if -I is also used.
- Display version and usage information.
To compile the program from source, simply run "make" in the source directory. Then copy the binary in your shell search path (or wherever you like). This text is also available as a man page in the ../doc/man/ directory, ready for copying it into the manual search path on unixoid systems.
An example session using the loader to create an input file and uploading it might look like this:
# shp2pgsql shaperoads roadstable roadsdb > roads.sql
# psql -d roadsdb -f roads.sql
A conversion and upload can be done all in one step using UNIX pipes:
# shp2pgsql shaperoads roadstable roadsdb | psql -d roadsdb
Originally written by Jeff Lounsbury <email@example.com>. Improved and maintained by Sandro Santilli <firstname.lastname@example.org>. Includes small contributions and improvements by others.
This application uses functionality from shapelib 1.2.9 by Frank Warmerdam <email@example.com> to read from ESRI Shape files.
More information is available at http://postgis.net