Create copy of a database in PostgreSQL

Correct way to copy entire database (its structure and data) to a new one in PostgreSQL.

CREATE DATABASE new_db WITH TEMPLATE original_db OWNER dbuser;

Example:

k2info=# CREATE DATABASE k2db WITH TEMPLATE k2info OWNER k2admin; | t --More-- CREATE DATABASE k2info=# \l List of databases Name | Owner | Encoding | Collate | Ctype | ICU | Access privileges -----------+--------------+----------+-------------+-------------+-----+------------------------------- edb | enterprisedb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | k2db | k2admin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | k2info | enterprisedb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | postgres | enterprisedb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | template0 | enterprisedb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | =c/enterprisedb + | | | | | | enterprisedb=CTc/enterprisedb template1 | enterprisedb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | =c/enterprisedb + | | | | | | enterprisedb=CTc/enterprisedb test | k2admin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | |

You can leave a response, or trackback from your own site.

Leave a Reply

Powered by k2schools