CLONE DATABASE IN POSTGRESQL

 Cloning a database is very easy in PostgreSQL. Use the below statement to clone the database.


CREATE DATABASE TEMPLATE = ;

Give the database name you want to clone in the place of Source database. PostgreSQL will take the database as a template and copy all the objects in that database to new database.

Example :-

postgres=# create database source;
CREATE DATABASE
postgres=# \c source
You are now connected to database “source” as user “postgres”.

source=# create table source(id text);
DEBUG:  building index “pg_toast_17241_index” on table “pg_toast_17241”
CREATE TABLE
source=# insert into source values(‘SOURCE DATABASE’);
INSERT 0 1
source=#
source=# create database TARGET TEMPLATE=source;
CREATE DATABASE

source=# \c target
You are now connected to database “target” as user “postgres”.
target=#
target=# \d
         List of relations
 Schema |  Name  | Type  |  Owner
——–+——–+——-+———-
 public | source | table | postgres
(1 row)

target=# select * from source;
       id
—————–
 SOURCE DATABASE

Comments

Popular posts from this blog

PostgreSQL - Architecture

PostgreSQL - Backup & Point In Time Recovery

Installing pgbadger on Linux for PostgreSQL log analysis