SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '[Database to copy]' AND pid <> pg_backend_pid(); CREATE DATABASE [Database to create] WITH TEMPLATE [Database to copy] OWNER [Your username];
Here is what the above code is Doing:
1. Terminate all connections to the database you want to copy.
2. Create a new database with the same structure as the old one.