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
PostgreSQL: dump and restore
提问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
If you want to restore to an empty database, you might want to run beforerestoring:
如果要还原到空数据库,则可能需要在还原之前运行:
DROP DATABASE IF EXISTS mydb;
CREATE DATABASE mydb;
The --clean
option for pg_dump
is not needed in this case.
在--clean
为选择pg_dump
在这种情况下没有必要的。
回答by Frank Heikens
回答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-owner
flag
为避免恢复时出现凭据和用户名问题,您可以使用--no-owner
标志
$ pg_dump --no-owner -U username -p 5432 dbname > filename-to-backup-to.sql