Download partial database from heroku

一世执手 提交于 2019-11-30 18:15:58

问题


I have a ruby on rails application hosted on heroku using postgresql as its database. Since the database is getting pretty large, I was wondering if there's a way to download only a specific part of it off of heroku. For example, is it possible to download only one specific table, or download only rows with parent_id == x.


回答1:


In addition to Steve's quite correct answer, you also have the option of connecting using psql to the DATABASE_URL and using \copy, e.g.

$ psql "$(heroku config:get DATABASE_URL)"

mydb=> \copy mytable TO 'mytable.csv' WITH (FORMAT CSV, HEADER)

mydb=> \copy (SELECT col1, col2 FROM mytable2 WHERE ...) TO 'mytable2_partial.csv' WITH (FORMAT CSV, HEADER)

You can extract whole tables, or the output of arbitrary queries (including joins etc). The table definition (DDL) is not exported this way, but can be dumped with pg_dump --schema-only -t ....




回答2:


Using the DATABASE_URL config setting you can use pg_dump to access your database and use the -t switch to specify a certain table.

For example, to export the table my_table into file called db.sql:

pg_dump -t my_table `heroku config:get DATABASE_URL` > db.sql

If you need to limit the download to certain rows then I don't think pg_dump will do the job on it's own. You could create another table in your Heroku database to first define the subset of rows that you want to download and then have pg_dump dump only that table. See this question for some ideas about how to do that: Export specific rows from a PostgreSQL table as INSERT SQL script



来源:https://stackoverflow.com/questions/30446068/download-partial-database-from-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!