/* KILL ALL EXISTING CONNECTION FROM ORIGINAL DB (sourcedb)*/SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activityWHERE 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;
它将终止与源数据库的所有连接以避免错误
ERROR: source database "SOURCE_DB" is being accessed by other users
cd /var/lib/pgsql/pg_dump database_name> database_name.out
重新分配数据库转储
psql -d template1CREATE DATABASE database_name WITH ENCODING 'UTF8' LC_CTYPE 'en_US.UTF-8' LC_COLLATE 'en_US.UTF-8' TEMPLATE template0;CREATE USER role_name WITH PASSWORD 'password';ALTER DATABASE database_name OWNER TO role_name;ALTER USER role_name CREATEDB;GRANT ALL PRIVILEGES ON DATABASE database_name to role_name;
CTR+D(logout from pgsql console)cd /var/lib/pgsql/
psql -d database_name -f database_name.out