Posted by & filed under Postgres & PostGIS.

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 :-)

pixelstats trackingpixel

2 Responses to “Automatically dumping all Postgres tables into their own SQL files”

  1. shenriod

    That is indeed very interesting, that’s exactly what I was trying to do!

    My only problem is that our server runs on Windows2003, not on Linux, so I can’t just copy-paste your code! And I am a total beginner when it comes to batch scripting… If you have time for it, would you be able to translate it to Windows? (I know I’m very demanding!). But no problem otherwise… I guess you have loads of other priorities!

    Thanks anyway for you fruitful blog… and for all your work for OpenGIS in general!

    • Tim Sutton

      Hi shenriod

      Thanks for the great comments. I don’t really use windows – you can probably do it by installing cygwin or msys, but doing it natively under a dos batch script is probably going to be very difficult since the DOS command environment is extremely limited when compared to the powerful bash shell.

      Regards

      Tim

Leave a Reply

You must be logged in to post a comment.