The simplest and fastest method to create a complete copy of an existing (live) database is to use CREATE DATABASE
with a TEMPLATE
:
CREATE DATABASE mydb_test TEMPLATE mydb;
然而,有<明星>进口限制违反了你的第二项要求:模板(来源)数据库不能与它有其他链接。 我引用该手册:。
It is possible to create additional template databases, and indeed one
can copy any database in a cluster by specifying its name as the
template for CREATE DATABASE
. It is important to understand, however,
that this is not (yet) intended as a general-purpose "COPY DATABASE
"
facility. The principal limitation is that no other sessions can be
connected to the source database while it is being copied. CREATE DATABASE
will fail if any other connection exists when it starts; during
the copy operation, new connections to the source database are prevented.
You can terminate all sessions to the template database if you have the necessary privileges with pg_terminate_backend()
.
To temporarily disallow reconnects, revoke the CONNECT
privilege (and GRANT
back later).
REVOKE CONNECT ON DATABASE mydb FROM PUBLIC;
-- while connected to another DB - like the default maintenance DB "postgres"
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = mydb -- name of prospective template db
AND pid <> pg_backend_pid(); -- don t kill your own session
CREATE DATABASE mydb_test TEMPLATE mydb;
GRANT CONNECT ON DATABASE mydb TO PUBLIC; -- only if they had it before
员额9.2使用<编码>procpid,而不是pid
:
相关:
如果你无力结束同时举行的届会,就象其他答复所建议的那样,着手计算<代码>pg_dump至psql
的产出。