PostGIS 2.0 pgsql2shp shp2pgsql Command Line Cheatsheet PDF Version

PostGIS 2.0.0 pgsql2shp shp2pgsql Cheat Sheet

shp2pgsql and pgsql2shp are all located in the bin folder of the PostgreSQL install.

pgsql2shp dumps a postgis database table, view or sql query to ESRI shape file format.

USAGE: pgsql2shp [OPTIONS] database [schema.]table pgsql2shp [OPTIONS] database query

shp2pgsql generates an SQL script from ESRI shape and DBF files suitable for loading into a PostGIS enabled database.

USAGE: shp2pgsql [OPTIONS] shapefile [schema.]table
New in 2.0.0 1, New in 1.52

General options: (P - pgsql2shp, S - shp2pgsql)

P
  S
  S
  S
  S
  S
  S
P  
P S
P
  S
  S
P S
  S
  S
  S
P
P
P
  S
P
  S
  S
  S
  S
  S
  S
  S
P
P S
  -b
  -s from_srid:to_srid 
  (-d|a|c|p) 
      -d   
      -a  
      -c  
      -p 
  -f filename
  -g geometry_column_name
  -h hostname 
  -D
  -e
  -k
  -i 
  -I 
  -p port
  -P password
  -r 
  -S
  -u user 
  -w  
  -W 
  -N 
  -n
  -G2  
  -T1
  -X1
  -m1 filename
  -? 
Use a binary cursor.
If -s :to_srid 1 is not specified then from_srid is assumed and no transformation happens.
These are mutually exclusive options:
Drops the table, then recreates it and populates it with current shape file data.
Appends shape file into current table, must be exactly the same table schema.
Creates a new table and populates it, default if you do not specify any options.
Prepare mode, only creates the table.
Use this option to specify the name of the file to create
Specify the name of the geometry column to be (S) created (P) exported.
Specify db server host name defaults to localhost.
Use postgresql dump format (defaults to sql insert statments).
Execute each statement individually, do not use a transaction. Not compatible with -D
Keep postgresql identifiers case.
Use int4 type for all integer dbf fields.
Create a GiST index on the geometry column.
Allows you to specify a database port other than the default. Defaults to 5432.
Connect to the database with the specified password.
Raw mode. Do not unescape attribute names and not skip the 'gid' attribute.
Generate simple geometries instead of MULTI geometries.
Connect to the database as the specified user. 
Use wkt format (for postgis-0.x support - drops M - drifts coordinates).
encoding The character encoding of Shape's attribute column. (default : "UTF-8")

policy Specify NULL geometries handling policy (insert,skip,abort)
Only import DBF file.
Use geography type instead of geometry (requires lon/lat data) in WGS84 long lat (-s SRID=4326)
Specify the tablespace for the new table. Indexes will still use the default tablespace unless the -X parameter is also used.
Specify the tablespace for the new index.
Remap identifiers to ten character names. The content of the file is lines of two symbols separated by a single white space.
Display this help screen

PSQL Connection options:

-h, --host=HOSTNAME database server host or socket directory -p, --port=PORT database server port number -U, --username=NAME connect as specified database user -W, --password force password prompt (should happen automatically) -e, --exit-on-error exit on error, default is to continue If no input file name is supplied, then standard input is used.

Loading data with shp2pgsql

Load data into PostgreSQL from ESRI shape file MA stateplane feet
shp2pgsql -s 2249 neighborhoods public.neighborhoods > neighborhoods.sql
psql -h myserver -d mydb -U myuser -f neighborhoods.sql

Do above in one step
shp2pgsql -s 4326 neighborhoods public.neighborhoods | psql -h myserver -d mydb -U myuser

Load data into PostgreSQL from ESRI shape file MA stateplane feet to geography
shp2pgsql -G -s 2249:4326 neighborhoods public.neighborhoods > neighborhoods_geog.sql
psql -h myserver -d mydb -U myuser -f neighborhoods_geog.sql

Sample linux sh script to load tiger 2007 massachusetts edges and landmark points
TMPDIR="/gis_data/staging"
STATEDIR="/gis_data/25_MASSACHUSETTS"
STATESCHEMA="ma"
DB="tiger"
USER_NAME="tigeruser"
cd $STATEDIR
#unzip files into temp directory
for z in */*.zip; do unzip -o -d $TMPDIR $z; done 
for z in *.zip; do unzip -o -d $TMPDIR $z; done

#prepare the tables don't load data
#force non-multi and set the geometry column name to the_geom_4269, dbf is in latin1 encoding
shp2pgsql -s 4269 -g the_geom_4269 -S -W "latin1" -p fe_2007_25025_edges.shp ${STATESCHEMA}.edges | psql -U $USER_NAME -d $DB
shp2pgsql -s 4269 -g the_geom_4269 -S -W "latin1" -p fe_2007_25025_pointlm.shp ${STATESCHEMA}.pointlm | psql -U $USER_NAME -d $DB

#loop thru pointlm and edges county tables and append to respective ma.pointlm ma.edges tables
for t in pointlm edges;
do
 for z in *${t}.dbf;
  do 
	shp2pgsql  -s 4269 -g the_geom_4269 -S -W "latin1" -a $z ${STATE_SCHEMA}.${t} | psql -d $DB -U $USER_NAME;  
  done
done

Outputing to ESRI Shapefile/DBF with pgsql2shp

Export query to a shape file called jpnei.shp/dbf
pgsql2shp -f "/path/to/jpnei" -h myserver -u apguser -P apgpassword mygisdb 
	"SELECT neigh_name, the_geom FROM neighborhoods WHERE neigh_name = 'Jamaica Plain'"
	
Export a table in ma schema called streets to streets.shp/dbf
pgsql2shp -f "/path/to/streets" -h myserver -u apguser -P apgpassword mygisdb ma.streets
This work is licensed under a Creative Commons Attribution Creative Commons.
Feel free to use this material for private or commercial purposes, but we ask that you please retain the http://www.bostongis.com Boston GIS website link.
Boston GIS     Paragon Corporation     Postgres OnLine Journal PostGIS in Action