Creating a new Postgres template database

One thing that is sometimes useful when you have multiple developers accessing the same Postgres database server is to setup your own template so that you can avoid contention. That way when someone is in the template goofing around your automated unit tests that create a new database don’t break.

To do that you create the template as a regular database then change it to a template by tweaking the internal postgres parameter. Note that you’ll need permission to do this of course.


$ createdb template_donotuse
$ psql template_donotuse
# update pg_database set datistemplate=true  where datname='template_donotuse';

For more information on templates the help is quite clear – http://www.postgresql.org/docs/8.0/interactive/manage-ag-templatedbs.html. In fact I figured this out from that page, I just want to have the commands somewhere convenient to copy paste from.

Leave a comment