Convert Shapefiles to PostGIS Tables

Document Information
Document ID: 
app2009-008

There is a relatively simple command that comes with PostGIS that allows you to convert ESRI shapefiles into geometry-enabled PostgreSQL tables. The following is a pair of commands that first converts the shapefile by creating an SQL statement to insert the records, and then pipes that output into a command which loads it into the appropriate database.

shp2pgsql -s <srid> <path to .shp file> <schema name>.<PostgreSQL table name> | psql -d <database name> -U <username>

An example command may look like this:

shp2pqsql -s 4326 /mnt/data-store/geoserver/gsvr/data/data/shapefiles/MajorElements_EarthChem.shp azgs.majorelements_earthchem | psql -d arizonageology -U gisuser

Run this command at a prompt on the machine that runs the PostgreSQL database. It will prompt you for the password of the user you've specified.

Comments

Where's shp2pgsql?

Ryan Clark's picture

If you followed the installation instructions for PostgreSQL and PostGIS that I wrote up, the shp2pgsql command is not available globally from the command line. Browse to /opt/postgis/loader before running the command shown above. This is the case for AMI's usgin-2.0 and up.