Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clone Database in PostgreSQL #clone #database #sql #postgresql
/* KILL ALL EXISTING CONNECTION FROM ORIGINAL DB (sourcedb)*/
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'SOURCE_DB' AND pid <> pg_backend_pid();
/* CLONE DATABASE TO NEW ONE(TARGET_DB) */
CREATE DATABASE TARGET_DB WITH TEMPLATE SOURCE_DB OWNER USER_DB;
--https://stackoverflow.com/questions/876522/creating-a-copy-of-a-database-in-postgresql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment