使用 PostgreSQL 的 PgAdmin-III 导出数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16784598/
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
Export DB with PostgreSQL's PgAdmin-III
提问by quarks
How to export a Postgresql db into SQL that can be executed into other pgAdmin
?
如何将 Postgresql db 导出到可以执行到其他 SQL 中pgAdmin
?
- Exporting as backup file, doesn't work when there's a difference in version
- Exporting as SQL file, does not execute when tried to run on a different
pgAdmin
- 导出为备份文件,版本不同时不起作用
- 导出为 SQL 文件,尝试在不同的服务器上运行时不执行
pgAdmin
I tried exporting a DB with pgAdmin III
but when I tried to execute the SQL in other pgAdmin
it throws error in the SQL, when I tried to "restore" a Backup file, it says there's a difference in version that it can't do the import/restore.
我尝试导出一个数据库,pgAdmin III
但是当我尝试以其他方式执行 SQL 时,pgAdmin
它会在 SQL 中引发错误,当我尝试“恢复”备份文件时,它表示版本存在差异,无法执行导入/恢复。
So is there a "safe" way to export a DB into standard SQL that can be executed plainly in pgAdmin
SQL editor, regardless of which version it is?
那么有没有一种“安全”的方法可以将数据库导出到标准 SQL 中pgAdmin
,无论它是哪个版本,都可以在SQL 编辑器中简单地执行?
回答by Craig Ringer
Don't try to use PgAdmin-III for this. Use pg_dump
and pg_restore
directly if possible.
不要尝试为此使用 PgAdmin-III。如果可能pg_dump
,pg_restore
直接使用和。
Use the version of pg_dump
from the destination server to dump the origin server. So if you're going from (say) 8.4 to 9.2, you'd use 9.2's pg_dump
to create a dump. If you create a -Fc
custom format dump (recommended) you can use pg_restore
to apply it to the new database server. If you made a regular SQL dump you can apply it with psql
.
使用pg_dump
来自目标服务器的版本转储源服务器。因此,如果您要从(例如)8.4 到 9.2,您将使用 9.2pg_dump
来创建转储。如果您创建-Fc
自定义格式转储(推荐),您可以将pg_restore
其应用于新的数据库服务器。如果您进行了常规 SQL 转储,则可以使用psql
.
See the manual on upgrading your PostgreSQL cluster.
Now, if you're trying to downgrade, that's a whole separate mess.
现在,如果你试图降级,那完全是一团糟。
You'll have a hard time creating an SQL dump that'll work in anyversion of PostgreSQL. Say you created a VIEW that uses a WITH
query. That won't work when restored to PostgreSQL 8.3 because it didn't support WITH
. There are tons of other examples. If you must support old PostgreSQL versions, do your development on the oldest version you still support and then export dumps of it for newer versions to load. You cannot sanely develop on a new version and export for old versions, it won't work well if at all.
您将很难创建可在任何版本的 PostgreSQL 中运行的 SQL 转储。假设您创建了一个使用WITH
查询的 VIEW 。当恢复到 PostgreSQL 8.3 时,这将不起作用,因为它不支持WITH
. 还有很多其他的例子。如果您必须支持旧的 PostgreSQL 版本,请在您仍然支持的最旧版本上进行开发,然后导出它的转储以加载新版本。你不能理智地开发一个新版本并导出旧版本,如果有的话它也不会很好地工作。
More troubling, developing on an old version won't always give you code that works on the new version either. Occasionally new keywords are added where support for new specification features are introduced. Sometimes issues are fixed in ways that affect user code. For example, if you were to develop on the (ancient and unsupported) 8.2, you'd have lots of problems with implicit casts to text on 8.3 and above.
更麻烦的是,在旧版本上开发也不总是能给您提供适用于新版本的代码。有时会添加新的关键字,以支持新的规范功能。有时问题会以影响用户代码的方式修复。例如,如果您要在(古老且不受支持的)8.2 上进行开发,那么在 8.3 及更高版本上隐式转换为文本时会遇到很多问题。
Your best bet is to test on all supported versions. Consider setting up automated testing using something like Jenkins CI. Yes, that's a pain, but it's the price for software that improves over time. If Pg maintained perfect backward and forward compatibility it'd never improve.
最好的办法是在所有支持的版本上进行测试。考虑使用 Jenkins CI 之类的东西来设置自动化测试。是的,这很痛苦,但随着时间的推移,软件的价格会有所提高。如果 Pg 保持完美的向后和向前兼容性,它永远不会改进。
回答by Somnath Muluk
Export/Import with pg_dump and psql
使用 pg_dump 和 psql 导出/导入
1.Set PGPASSWORD
1.设置PGPASSWORD
export PGPASSWORD='123123123';
2.Export DB with pg_dump
2.使用 pg_dump 导出数据库
pg_dump -h <<host>> -U <<username>> <<dbname>> > /opt/db.out
/opt/db.out is dump path. You can specify of your own.
/opt/db.out 是转储路径。您可以指定自己的。
3.Then set again PGPASSWORD of you another host. If host is same or password is same then this is not required.
3.然后再设置你另一台主机的PGPASSWORD。如果主机相同或密码相同,则不需要。
4.Import db at your another host
4.在你的另一台主机上导入数据库
psql -h <<host>> -U <<username>> -d <<dbname>> -f /opt/db.out
If username is different then find and replace with your local username in db.out file. And make sure on username is replaced and not data.
如果用户名不同,则在 db.out 文件中查找并替换为您的本地用户名。并确保用户名被替换而不是数据。
If you still want to use PGAdmin then see procedure below.
如果您仍想使用 PGAdmin,请参阅以下步骤。
Export DB with PGAdmin:
使用 PGAdmin 导出数据库:
Select DB and click Export.
选择 DB 并单击导出。
- File Options
- Name DB file name for you local directory
- Select Format - Plain
- Ignore Dump Options #1
- Dump Options #2
- Check
Use Insert Commands
- Check
- Objects
- Uncheck tables if you don't want any
- 文件选项
- 为您的本地目录命名 DB 文件名
- 选择格式 - 普通
- 忽略转储选项 #1
- 转储选项 #2
- 查看
Use Insert Commands
- 查看
- 对象
- 如果您不想要任何表格,请取消选中
Import DB with PGAdmin:
使用 PGAdmin 导入数据库:
- Create New DB.
- By keeping selected DB, Click
Menu->Plugins->PSQL Console
Type following command to import DB
\i /path/to/db.sql
- 创建新数据库。
- 通过保持选定的数据库,单击
Menu->Plugins->PSQL Console
输入以下命令导入数据库
\i /path/to/db.sql
If you want to export Schema and Data separately.
如果要分别导出 Schema 和 Data。
Export Schema
导出架构
- File Options
- Name schema file at you local directory
- Select Format - Plain
- Dump Options #1
- Check
Only Schema
- Check
Blobs
(By default checked)
- Check
- 文件选项
- 在您的本地目录中命名架构文件
- 选择格式 - 普通
- 转储选项 #1
- 查看
Only Schema
- 勾选
Blobs
(默认勾选)
- 查看
Export Data
导出数据
- File Options
- Name data file at you local directory
- Select Format - Plain
- Dump Options #1
- Check
Only Data
- Check
Blobs
(By default checked)
- Check
- Dump Options #2
- Check
Use Insert Commands
- Check
Verbose messages
(By default checked)
- Check
- 文件选项
- 在本地目录中命名数据文件
- 选择格式 - 普通
- 转储选项 #1
- 查看
Only Data
- 勾选
Blobs
(默认勾选)
- 查看
- 转储选项 #2
- 查看
Use Insert Commands
- 勾选
Verbose messages
(默认勾选)
- 查看
Note:It takes time to Export/Import based on DB size and with PGAdmin it will add some more time.
注意:根据数据库大小导出/导入需要时间,使用 PGAdmin 会增加一些时间。