在PostgreSQL中创建数据库的副本 [英] Creating a copy of a database in PostgreSQL

查看:96
本文介绍了在PostgreSQL中创建数据库的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What's the correct way to copy entire database (its structure and data) to a new one in pgAdmin?

推荐答案

Postgres允许在创建新数据库时将服务器上的任何现有数据库用作模板.我不确定pgAdmin是否在创建数据库对话框中为您提供了该选项,但是如果没有,您应该能够在查询窗口中执行以下操作:

Postgres allows the use of any existing database on the server as a template when creating a new database. 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:

CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;

还是,您可能会得到:

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();

这篇关于在PostgreSQL中创建数据库的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆