PostgreSQL:转储和恢复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10337095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:51:45  来源:igfitidea点击:

PostgreSQL: dump and restore

postgresql

提问by Dmitry

I use EMS SQL Manager for PostgreSQL and I need to dump difficult database(domains, 300+ stored procedures/functions, triggers, data, etc). This tool cannot do it.

我使用 EMS SQL Manager for PostgreSQL,我需要转储困难的数据库(域、300 多个存储过程/函数、触发器、数据等)。这个工具做不到。

Please advice me good GUI tool for postgres.

请建议我使用 postgres 的好 GUI 工具。

回答by Erwin Brandstetter

You can always just use the command lineutility.
Dump the cluster:

您始终可以只使用命令行实用程序。
转储集群:

pg_dumpall -p 5432 > /path/to/my/dump_file.sql

Dump a single database:

转储单个数据库:

pg_dump -p 5432 mydb > /path/to/my/mydb_dump.sql

Dump the schema only:

仅转储架构:

pg_dump -p 5432 mydb -s > /path/to/my/mydb_dump_schema.sql

More in the manual.

手册中有更多内容。



If you want to restore to an empty database, you might want to run beforerestoring:

如果要还原到空数据库,则可能需要还原之前运行:

DROP DATABASE IF EXISTS mydb;
CREATE DATABASE mydb;

The --cleanoption for pg_dumpis not needed in this case.

--clean为选择pg_dump在这种情况下没有必要的。

回答by Frank Heikens

pgAdmin3will do the trick, it has pg_dump and pg_restore included in the installer.

pgAdmin3可以解决这个问题,它在安装程序中包含 pg_dump 和 pg_restore。

回答by Dinesh Pallapa

Backup your database no tool needed.we can do with terminal

无需工具即可备份您的数据库。我们可以使用终端

All commands should be run as the postgres user.

所有命令都应以 postgres 用户身份运行。

 sudo su - postgres 

Backup a single database

备份单个数据库

pg_dump db_name > db_backup.sql

Restore a single database

恢复单个数据库

psql db_name < db_backup.sql

Backup an entire postgres database cluster

备份整个 postgres 数据库集群

pg_dumpall > cluster_backup.sql

Restore an entire postgres database cluster

恢复整个 postgres 数据库集群

psql -f cluster_backup.sql postgres

Refer this source for more commands backup commands

有关更多命令备份命令,请参阅此来源

回答by U?ur Y?lmaz

pg_dump -U uguryilmaz modaltrans_dev > backup.sql

pg_dump -U uguryilmaz modaltrans_dev > 备份.sql

回答by Shubham

if you use md5 authentication technique and want to use a specific user to get db dump, you can do

如果您使用 md5 身份验证技术并希望使用特定用户来获取数据库转储,您可以这样做

$ pg_dump -U username -p 5432 dbname > filename-to-backup-to.sql

To avoid credential and username issues while restoring, you can use --no-ownerflag

为避免恢复时出现凭据和用户名问题,您可以使用--no-owner标志

$ pg_dump --no-owner -U username -p 5432 dbname > filename-to-backup-to.sql