Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

postgresql - 在PostgreSQL中创建数据库的副本(Creating a copy of a database in PostgreSQL)

在pgAdmin中将整个数据库(其结构和数据)复制到新数据库的正确方法是什么?

  ask by egaga translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Postgres allows the use of any existing database on the server as a template when creating a new database.

(创建新数据库时,Postgres允许使用服务器上的任何现有数据库作为模板。)

I'm not sure whether pgAdmin gives you the option on the create database dialog but you should be able to execute the following in a query window if it doesn't:

(我不确定pgAdmin是否在创建数据库对话框中为您提供了该选项,但是如果没有,您应该能够在查询窗口中执行以下操作:)

CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;

Still, you may get:

(不过,您可能会得到:)

ERROR:  source database "originaldb" is being accessed by other users

To disconnect all other users from the database, you can use this query:

(要将所有其他用户与数据库断开连接,可以使用以下查询:)

SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity 
WHERE pg_stat_activity.datname = 'originaldb' AND pid <> pg_backend_pid();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...