postgresql 如何将二进制 pgdump(压缩)转换为普通 SQL 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21429335/
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-09-11 00:35:56  来源:igfitidea点击:

How do I convert a binary pgdump (compressed) to a plain SQL file?

postgresqlpg-dumppg-restore

提问by sorin

I do want to search for some data inside a database dump but these dumps are using the binary-compressed format (PGDMPheader).

我确实想在数据库转储中搜索一些数据,但这些转储使用的是二进制压缩格式(PGDMP标头)。

How can I convert these to SQL without restoring them?

如何在不恢复它们的情况下将它们转换为 SQL?

回答by Craig Ringer

pg_restore, when run without a database name, outputs a text dump to stdout; you can send that elsewhere with -for with I/O redirection.

pg_restore, 在没有数据库名称的情况下运行时,将文本转储输出到标准输出;您可以使用-f或使用 I/O 重定向将其发送到其他地方。

pg_restore -f mydatabase.sql mydatabase.dump 

回答by knownasilya

The fastest method that I've used was:

我用过的最快的方法是:

pg_restore mybinaryfile.backup > mysqlfile.sql

No special flags, since pg_restore just spits it out to stdout.

没有特殊标志,因为 pg_restore 只是将它吐出到标准输出。

回答by Alexis Wilke

Note that if you run multiple clusters, the restore command may not like the default version...

请注意,如果您运行多个集群,则恢复命令可能不像默认版本...

pg_restore: [archiver] unsupported version (1.12) in file header

pg_restore: [archiver] 文件头中不支持的版本 (1.12)

In that case you have to specify the version, host and port as in:

在这种情况下,您必须指定版本、主机和端口,如下所示:

pg_restore --cluster 9.1/localhost:5433 -f db.sql db.pgsql

(note that the host:portinfo is ignored with the -f option.)

(请注意,host:port-f 选项会忽略该信息。)

The port (5433) can be determined using the pgsqlcommand as in:

可以使用如下pgsql命令确定端口 (5433) :

pgsql --port 5433 template1

When pgsqlconnects, it writes a comment such as:

pgsql连接时,它写入一个注释,例如:

psql (9.3.6, server 9.1.13)

psql(9.3.6,服务器 9.1.13)

This means you are running pgsql 9.3.6 and that port 5433 references server 9.1.13.

这意味着您正在运行 pgsql 9.3.6 并且端口 5433 引用服务器 9.1.13。

If you are not sure which ports are currently used, you may use the netstatcommand as in:

如果您不确定当前使用了哪些端口,您可以使用如下netstat命令:

sudo netstat -a64np | grep LISTEN | grep postgres

The sudois required for the -poption which prints the process name. That gives you a list of ports (usually TCP and UDP ports).

sudo是必需的-p,其打印进程名称选项。这会给你一个端口列表(通常是 TCP 和 UDP 端口)。

Finally, on a Debian/Ubuntu system, you can get a list of installed clusters with the dpkg -lcommand as in:

最后,在 Debian/Ubuntu 系统上,您可以使用以下dpkg -l命令获取已安装集群的列表:

dpkg -l '*postgres*'

The list of entries that start with 'ii' (left most column) are currently installed. You, of course, have similar commands for other Unices to help you determine installed versions.

当前已安装以“ii”(最左侧的列)开头的条目列表。当然,您可以使用其他 Unices 的类似命令来帮助您确定已安装的版本。