Create a PostGIS DB with Make

Sometimes I find myself cycling through databases when developing with PostGIS. To reduce the amount of typing I've written a simple makefile. It's cleaner than a shell script:

POSTGIS = /usr/local/share/postgresql/contrib
DBNAME = new_db

all: db

clean:
    dropdb $(DBNAME)

db:
    createdb $(DBNAME)
    createlang plpgsql $(DBNAME)
    psql -d $(DBNAME) -c "CREATE SCHEMA postgis"
    psql -d $(DBNAME) -f $(POSTGIS)/lwpostgis.sql
    psql -d $(DBNAME) -f $(POSTGIS)/spatial_ref_sys.sql

The command below creates a fresh database:

$ PGUSER=postgres make -f makefile_postgis

and the following drops it:

$ PGUSER=postgres make -f makefile_postgis clean

You can easily add other targets that use shp2pgsql and psql to load shapefiles into the new database. See the PCL makefile_postgis for example.

Comments

Re: Create a PostGIS DB with Make

Author: Mateusz Loskot

Oh, Irony! It's such obvious to use Make to almost every repetitive task, however I've never bethought about it :-) Sean, I think this could be a very nice improvement to PostGIS makefiles to automation final tasks: createlang plpgsql yourtestdatabase psql -d yourtestdatabase -f lwpostgis.sql psql -d yourtestdatabase -f spatial_ref_sys.sql what do you think?