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

查看:20
本文介绍了在 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天全站免登陆