Clone schema in PostgreSQL

renatosuero

Renato Suero

Posted on January 5, 2019

Clone schema in PostgreSQL

Hi everyone,

My goal is to keep the post short, so If you don't know what is schema access this link.

Schemas can be useful to clients, maybe a schema by the client or schema by "category". In my job(our business is based in soccer) we use schemas by championship and year.
A sample :
ucl_2018
ucl_2017
championship *that's the model

Ok, now we need to create a new schema ucl_2019, right?

I researched and after some time, I found a function to help me, but I needed to do changes. I didn't remember exactly what I needed to do, so I decided to share the function I've been using. Another thing this function will create everything indexes, keys, foreign keys, etc...

Ok, that is the function you need to run in your database.

After that, you only need to run

select clone_schema('source','destination')
Enter fullscreen mode Exit fullscreen mode

In my case,

select clone_schema('championship','ucl_2019');
Enter fullscreen mode Exit fullscreen mode

I hope it can be useful to you. I love PostgreSQL, so I want to write more about other features I like or I think that can be useful.
My new post will be a sequence for that, I'll show how to update the schemas, running migrations in the schemas. I wrote the post

💖 💪 🙅 🚩
renatosuero
Renato Suero

Posted on January 5, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related