Importing and Exporting


  • Attribute data stored with a shapefile (the .dbf file) can only use column names shorter than 14 characters. If you try and export a postGIS table that has column names longer than 14 characters, they will be truncated when converted to a shapefile.
  • If a column in postGIS is defined as numeric, ogr2ogr will not correctly interpret the values in this column: i.e. floating point values less than 1 will be truncated to 0. In order to avoid this problem always define your colums as double precision or float. For dynamically generated tables, cast affected columns to a defined numeric precision: select colum_a::numeric(7,3) where '7' refers to the total number of digits in the column, and '3' refers to the number of decimal places.

Geometry and attribute data to GIS file format

 GDAL/OGR tools This approach allows simultaneous conversion of coordinate systems, but is less flexible with respect to generation of new tables in PostGIS.

ogr2ogr -f "PostgreSQL" PG:'dbname=ssurgo_combined user=xxxx password=xxxx' input_file.shp

ogr2ogr output_file.shp PG:'dbname=ssurgo_combined user=xxxx password=xxxx' tablename

 Note that tables must be correctly 'registered' in the geometry_columns table for this to work:

INSERT INTO geometry_columns VALUES ('','public','tablename','wkb_geometry',2,SRID,'geomtype');

PostGIS Loader/Dumper This approach is the simplest, but does not allow on-the-fly conversion of coordinate systems.

shp2pgsql -s SRID -c -g wkb_geometry -I shapefile.shp schema.table | psql -U username -h host database

Note that SRID is the PostGIS 'spatial ref. sys. id' (see the spatial_ref_sys table). See the manual page for shp2pgsql for a complete list of arguments and their meanings.

pgsql2shp -f shapefile.shp -h host -u username -P password -k -g wkb_geometry database schema.table

See the manual page for pgsql2shp for a complete list of arguments and their meanings.

Where tablename is your newly created table, SRID is the SRID (spatial reference ID) for the geometry in this table, and geomtype is the type of geometry: POINT, LINE, POLYGON, etc.

Attribute data to text format

CSV format, from within the psql client

\copy tablename TO 'filename.csv' CSV HEADER

CSV format, via psql client

echo "select column_list from table_list " | psql --tuples --no-align  -F ","  database > file.csv

Tabular data to HTML format, via psql clientSee output below:

echo "select column_list from table_list " | psql --html database > file.html

HTML output from psql

132472.230854819 Hilmar variant
322819.967391312 Oneil
362729.418301135 Carranza
431948.171760353 Tuff rockland
448784.927049035 Gravel pits
500763.225267798 Snelling variant
518860.954990617 Foster
571640.132661382 Alamo
648973.748756059 Toomes
924327.631201791 Dumps