Skip to content
Advertisement

Duplicate postgresql schema including sequences

My database layout needs to create new schema for each new customer. Currently I use internal function I found on the net and modified a little bit.

The problem with this script is that tables in new schema continue to use source schema’s sequences. Is there way using sql statements (or other reliable way) to get fresh copy of sequences (or even another reliable way to duplicate entire schema) for the newly created tables?

Advertisement

Answer

And so after some thinking I went along with updating sql function mentioned in my first post so now it looks like this:

This is not quite universal solution for everybody, but as all my tables in schema have serial field named id, it fits me.

Version suggested by @erwin-brandstetter with dump / hack dump file / restore dump file back again is commonly seen on the forums as the way to go.

In case of dedicated server it could work, in case of shared hosting (or need of less dependencies on outside scripts) the way of internal function seems better.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement