Create a copy of database in PostgreSQL

PostgreSQL allows us to use of any existing database on the server as a template when creating a new database.

Syntax to Create a copy of database in PostgreSQL

CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;

If we run the above command, it take all objects of originaldb to newdb.

Example:

CREATE DATABASE company_new WITH TEMPLATE company OWNER postgres;

If we run the above command, it take all objects of company to newly created company_new database.

Verification:

Run either \l or select datname from pg_database;

ompany_new=# \l
List of databases

Name | Owner | Encoding | Collate | Ctype | Access privileges -------------+----------+----------+----------------------------+----------------------------+----------------------- company | k2admin | UTF8 | English_United States.1252 | English_United States.1252 | company_new | postgres | UTF8 | English_United States.1252 | English_United States.1252 | postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 | template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres + | | | | | postgres=CTc/postgres

company_new=# select datname from pg_database;

datname ------------- postgres template1 template0 company company_new (5 rows)

CheapSexCams
Responses are currently closed, but you can trackback from your own site.

Comments are closed.

Powered by k2schools
%d bloggers like this: