Someone asked on twitter it is possible to dump all the tables in Postgres to individual shp files. Some time ago I wrote a script to dump all tables as SQL dumps. The question prompted me to tweak that script to drop out shapefiles instead.
My original script looked like the listing below. The dump files contain data only (see the comments in the bash script below) because I use this script to create fixtures for my django projects.
#!/bin/bash
# A script to create sql formatted fixtures (serialised models)
# used to initialise the application if you install it to another
# machine. You should run this any time you change your models
# or when you need to make a backup of all your data.
# Tim Sutton 2009
mkdir bees/sql
for TABLE in `echo "\d" | psql sabio | grep -v seq | awk '{print $3}'`
do
echo $TABLE
# -a data only
# -t table
# -D dump as sql inserts
pg_dump -a -t $TABLE -D sabio > bees/sql/${TABLE}.sql
#bzip2 bees/sql/${TABLE}.sql
done
To make the script drop out shapefiles I modified it a bit as shown in the next listing. Obviously as we are dumping shapefiles, we should only bother dumping tables with geometry in them so I went the route of using the geometry_columns table to decide which tables to dump…
#!/bin/bash # A script to dump shapefiles of all tables listed in geometry_columns # Tim Sutton 2009 mkdir bees/sql for TABLE in `echo "select f_table_name from geometry_columns;" | psql sabio \ | head -n -2 | egrep -v "\-\-\-\-\-\-\-\-\-" | egrep -v "f_table_name"` do echo $TABLE pgsql2shp sabio $TABLE done
Hope this is useful to someone out there



Comments
Leave a comment Trackback