Discussion:
cloning database
Philipp Kraus
2014-09-19 06:35:33 UTC
Permalink
Hello,

I need around 150 copies of a database (for an exam). I have got a
database with tables and data and for my exam I would copy this
database in his way:

database_source

database1
database2
….
database150

Is there a buildin way to clone the "database_source" with all
structure and data into a new database "database1..150" ?

Thanks a lot

Phil
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Alban Hertroys
2014-09-19 07:12:20 UTC
Permalink
Post by Philipp Kraus
Hello,
database_source
database1
database2
….
database150
Is there a buildin way to clone the "database_source" with all structure and data into a new database "database1..150" ?
There is. You can use database_source as the template for creating new databases. There are a few restrictions, like the template database not being active, for example. See the docs for CREATE DATABASE and the createdb command.

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Albe Laurenz
2014-09-19 07:19:48 UTC
Permalink
Post by Philipp Kraus
I need around 150 copies of a database (for an exam). I have got a
database with tables and data and for my exam I would copy this
database_source
database1
database2
….
database150
Is there a buildin way to clone the "database_source" with all
structure and data into a new database "database1..150" ?
CREATE DATABASE database1 TEMPLATE database_source;

Yours,
Laurenz Albe
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http:/
hubert depesz lubaczewski
2014-09-19 11:04:36 UTC
Permalink
Post by Philipp Kraus
Is there a buildin way to clone the "database_source" with all structure
and data into a new database "database1..150" ?
assuming you're using bash shell, this should work:

for i in {1..150}; do createdb -T database_source database$i; done

it's not a built-in, but very close.

depesz
Craig Ringer
2014-09-19 12:50:54 UTC
Permalink
Post by hubert depesz lubaczewski
for i in {1..150}; do createdb -T database_source database$i; done
Unless `datistemplate` is set for the database, you'll need to do this
as a superuser.
--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Philipp Kraus
2014-09-20 15:50:52 UTC
Permalink
On Fri, Sep 19, 2014 at 8:35 AM, Philipp Kraus
Is there a buildin way to clone the "database_source" with all
structure and data into a new database "database1..150" ?
for i in {1..150}; do createdb -T database_source database$i; done
it's not a built-in, but very close.
depesz
Thanks this is nice, to define a template and run the shell script

Thanks a lot

Phil
David G Johnston
2014-09-20 18:46:53 UTC
Permalink
On Fri, Sep 19, 2014 at 8:35 AM, Philipp Kraus <
>
Post by Philipp Kraus
Is there a buildin way to clone the "database_source" with all structure
and data into a new database "database1..150" ?
for i in {1..150}; do createdb -T database_source database$i; done
it's not a built-in, but very close.
depesz
Any advantage to this compared to using pl/pgsql via a DO command while
connected to the postgres database? Note it would require dynamic SQL
(i.e., EXECUTE).

David J.






--
View this message in context: http://postgresql.1045698.n5.nabble.com/cloning-database-tp5819599p5819805.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Loading...