Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Script to create a copy of the Existing Database

 I am sharing a script to make a copy of the PostgreSQL database.

We can also create copies of the database using the pgAdmin tool, but I always preferred to use and create SQL script to perform the Database Administrator related activities.

In the development environment, I am following a practice that after every release of the sprint, I take backup of the database and as well as create the previous version of the database.

If any user wants to check existing functionality in the previous version, they can test with an old version of the database.

Below is a script to make a copy of an existing database:
CREATE DATABASE newDB WITH TEMPLATE oldDB OWNER dbUser;
After executing this,
If you get any error, you should kill all the running sessions of that existing database.
ERROR:  source database "originaldb" is being accessed by other users
You can use below script to kill all running sessions:
You can also visit this article.
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = 'datbase_name'
  AND pid <> pg_backend_pid();

Comments

Popular posts from this blog

How to find the server is whether standby (slave) or primary(master) in Postgresql replication ?

7 Steps to configure BDR replication in postgresql

How to Get Table Size, Database Size, Indexes Size, schema Size, Tablespace Size, column Size in PostgreSQL Database

Ora2PG - Oracle/MySQL to Postgres DB migration Version 20.0

PostgreSQL Introduction