postgresql 如何在 heroku 上获得纯文本 postgres 数据库转储?

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

How can I get a plain text postgres database dump on heroku?

sqlpostgresqlheroku

提问by user1187534

Due to version incompatibilities of my postgres database on heroku (9.1) and my local installation (8.4) I need a plain text sql database dump file so I can put a copy of my production data on my local testing environment.

由于我的 postgres 数据库在 heroku (9.1) 和我的本地安装 (8.4) 上的版本不兼容,我需要一个纯文本的 sql 数据库转储文件,以便我可以将我的生产数据的副本放在我的本地测试环境中。

It seems on heroku I can't make a dump using pg_dump but can instead only do this:

在 heroku 上,我似乎无法使用 pg_dump 进行转储,而只能这样做:

$ heroku pgbackups:capture
$ curl -o my_dump_file.dump `heroku pgbackups:url`

...and this gives me the "custom database dump format" and not "plain text format" so I am not able to do this:

...这给了我“自定义数据库转储格式”而不是“纯文本格式”,所以我无法做到这一点:

$ psql -d my_local_database -f my_dump_file.sql

回答by Alex

You could just make your own pg_dump directly from your Heroku database.

你可以直接从你的 Heroku 数据库制作你自己的 pg_dump。

First, get your postgres string using heroku config:get DATABASE_URL.

首先,使用heroku config:get DATABASE_URL.

Look for the Heroku Postgres url (example: HEROKU_POSTGRESQL_RED_URL: postgres://user3123:[email protected]:6212/db982398), which format is postgres://<username>:<password>@<host_name>:<port>/<dbname>.

查找 Heroku Postgres url(例如:)HEROKU_POSTGRESQL_RED_URL: postgres://user3123:[email protected]:6212/db982398,格式为postgres://<username>:<password>@<host_name>:<port>/<dbname>.

Next, run this on your command line:

接下来,在命令行上运行它:

pg_dump --host=<host_name> --port=<port> --username=<username> --password --dbname=<dbname> > output.sql

The terminal will ask for your password then run it and dump it into output.sql.

终端将询问您的密码,然后运行它并将其转储到 output.sql 中。

Then import it:

然后导入:

psql -d my_local_database -f output.sql

回答by Leigh Brenecki

Heroku's PGBackups actually uses pg_dump behind the scenes, and the "custom format" is actually pg_dump's custom format (-Fcparameter), not Heroku's own custom format.

Heroku 的 PGBackups实际上在幕后使用了 pg_dump,而“自定义格式”实际上是pg_dump 的自定义格式(-Fc参数),而不是 Heroku 自己的自定义格式。

This means you can use pg_restore, which is part of Postgres, to restore your Heroku backup into another database directly:

这意味着您可以使用pg_restorePostgres 的一部分 将您的 Heroku 备份直接恢复到另一个数据库中:

pg_restore -d mydatabase my_dump_file.dump

In addition, if you call pg_restorewithout specifying a database to restore to, it'll print SQL statements to standard out, so you can turn your Heroku backup into a SQL file that way:

此外,如果您调用时pg_restore没有指定要恢复到的数据库,它会将 SQL 语句打印到标准输出,因此您可以通过这种方式将 Heroku 备份转换为 SQL 文件:

pg_restore my_dump_file.dump > sql_statements.sql

回答by tobias.mcnulty

Assuming you have a DATABASE_URLconfigured in your environment, there is a far simpler method:

假设您DATABASE_URL在环境中配置了一个,则有一个简单得多的方法:

heroku run 'pg_dump $DATABASE_URL' > my_database.sql

This will run pg_dumpin your container and pipe the contents to a local file, my_database.sql. The single quotes are important.If you use double quotes (or no quotes at all), DATABASE_URLwill be evaluated locally rather than in your container.

这将pg_dump在您的容器中运行并将内容通过管道传输到本地文件my_database.sql. 单引号很重要。如果您使用双引号(或根本没有引号),DATABASE_URL将在本地而不是在您的容器中进行评估。

If your whole purpose is to load the contents into a local database anyways, you might as well pipe it straight there:

如果您的整个目的是将内容加载到本地数据库中,您不妨直接在那里进行管道传输:

createdb myapp_devel  # start with an empty database
heroku run 'pg_dump -xO $DATABASE_URL' | psql myapp_devel

The addition of -xOavoids dumping GRANT, REVOKE, and ALTER OWNERstatements, which probably don't apply to your local database server. If any of your COPYcommands fail with the error ERROR: literal carriage return found in data(mine did), see this answer.

添加-xO可避免转储GRANTREVOKEALTER OWNER语句,这些语句可能不适用于您的本地数据库服务器。如果您的任何COPY命令因错误而失败ERROR: literal carriage return found in data(我的),请参阅此答案

It's quite possible this didn't work two and a half years ago when this question was originally asked, but for those looking for a way to easily get a dump of your Heroku Postgres database, this appears to be the simplest possible way to do this today.

两年半前最初提出这个问题时,这很可能不起作用,但是对于那些正在寻找一种轻松获取 Heroku Postgres 数据库转储的方法的人来说,这似乎是最简单的方法今天。

回答by Edd

Heroku pg:backups:capture
Heroku pg:backups:download

Taken from https://devcenter.heroku.com/articles/heroku-postgres-import-export. Now you have a binary file. To obtain the file in plain text format, the following worked for me. Note: You will need to install PostgreSQL.

摘自https://devcenter.heroku.com/articles/heroku-postgres-import-export。现在你有一个二进制文件。要以纯文本格式获取文件,以下内容对我有用。注意:您需要安装 PostgreSQL。

pg_restore latest.dump > latest.sql